0% found this document useful (0 votes)
59 views3 pages

GoLang Tutorial Examples Guide

Go allows variables to be declared without initialization, giving them default values. Variables can be declared using the var keyword and specifying the data type, or inferred using a short variable declaration :=. Common data types in Go include numbers with a default of 0, strings with an empty default, arrays with a fixed size, slices which are dynamically sized arrays, and maps for storing key-value pairs. Flow control in Go includes if/else statements and for loops that can be used traditionally or as a while loop. Functions can return multiple values and errors.

Uploaded by

Javi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

GoLang Tutorial Examples Guide

Go allows variables to be declared without initialization, giving them default values. Variables can be declared using the var keyword and specifying the data type, or inferred using a short variable declaration :=. Common data types in Go include numbers with a default of 0, strings with an empty default, arrays with a fixed size, slices which are dynamically sized arrays, and maps for storing key-value pairs. Flow control in Go includes if/else statements and for loops that can be used traditionally or as a while loop. Functions can return multiple values and errors.

Uploaded by

Javi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

# [Go](https://golang.

org)

## Variables

If variables are declared without initialization, they are given a value by


default.

The general way of declaring variables:

```go
var var_name1 data_type
var var_name2 data_type = value
```

Go can infer the data types by using the following syntax:

```go
var_name := value
```

### Numbers

Default value is 0.

```go
var x int
y := 5
var z = x + y // 0 + 5
```

### Strings

Default value is the empty string.

```go
name := "John"
```

### Arrays

Arrays have a fixed size and are zero indexed.

```go
var a [5]int // Create an integer array of size 5
a[2] = 7 // Set the element with index 2 to the value 7

b := [2]string{"Penn", "Teller"}
```

### Null value

```go
nil
```

### Slices

Slices are dynamically sized arrays.


```go
var s []int{1, 2, 3, 4, 5}
s = append(s, 6)
```

### Maps

```go
vertices := make(map[string]int)

vertices["triangle"] = 2
vertices["square"] = 3
vertices["dodecagon"] = 12

delete(vertices, "triangle")
```

## Flow Control

### If / Else statements

```go
x := 5

if x > 5 {
// do something
} else {
// do other thing
}
```

### For

The for loop can be used as the traditional for and for each, but also as a while
loop.

```go
for i := 0; i < 5; i++ {
print(i)
}
```

```go
letters := []string{"a", "b", "c"}
for index, value := range letters {
println("index:", index, "value:", value)
}
```

```go
i := 0
for i < 5 {
print(i)
i++
}
```

## Functions
Function with no return value:

```go
func foo() {
// do somethng
}
```

Function with one return value:

```go
func sum(x int, y int) int {
return x + y
}
```

Functions in go can have multiple return values

```go
import (
"errors"
"math"
)

func sqrt(x float64) (float64, error) {


if x < 0 {
return 0, errors.New("Undefined for negative numbers.")
}
return math.Sqrt(x), nil
}

func other() {
result, err := sqrt(16)
}
```

You might also like