Variables, types and Keywords in go
Tags: go, beginners, programming
Published: 2023-09-20 17:06:43

In the blog, I want to look into variables, basic types, keywords and control structures of our go language.

### Variables:

The variable is a name given to a storage area that the program can manipulate. The variable is like a container and the name of the variable is like a label that shows what is inside the container. Go is a strongly, statically typed language which means variable types need to be explicitly declared and thus determined at compile time.

```go
var a = 100
const b = "I am a String"
var isTrue = false
```

Here the `var` and `const` are keywords. When a variable is declared with the `var` keyword can change the value after the declaration. When a variable is declared with the `const` keyword, the value remains the same throughout the life of the program.

Notice that we are not mentioning the type of the variable in the above examples. When we declare a variable without specifying any type, The variable type is inferred from the value of the right-hand side which is known as `Type inference.`

we can specify the type of the variable as follows

```go
var a int = 200
const b string = "I am Mr.B"
var isBool bool
var price int
var school string
// till now the values of isBool => false, school => "", price => 0
// later point in the programming.

isBool = true
price = 456
school = "New Bay School"
```

When we don't want to assign the variable value while declaring, we must mention the type of the variable. Every variable declared should be used at least once in the program, the compiler throws an error. The value of the constants should be known by compile time. The values of `var` can be known at run time.

```go
package main
import("fmt")
func main(){
    a := 100
    fmt.Println(a)
}
```

the above line `a:=100` is a shorthand notation of `var a = 100` or `var a int = 100`. the `:=` means the variable is declared and assigned with a right-hand side value. This shorthand representation can only be done inside a function and you cannot use `:=` for the same variable.

#### Declaring Multiple Variables:

In Go, it is possible to declare multiple variables in the same line.

```go
var a, b, c int = 1, 2, 3
```

```go
var a, b = "Hello" , 3
c, d : = false , "World !"
```

Multiple variable declarations can be grouped.

```go
var (
    a int
    b int = 1
    c string = "Go Gophers!"
)
```

### DataTypes:

The data type specifies the type and size of the variable values. There are 3 basic basic types in go:

* **bool**: represents a boolean value either *true* or *false.*
    

```go
var A bool //'A' will have a false value by default.
A = true
var B = false
C := true
```

* **Numeric**: represents an Integer type, floating type and complex type.
    

Integer: An integer is used to store a whole number without decimal points. By default integer is `int` which takes the appropriate size of the machine i.e., `32 bits` for a 32-bit machine and `64 bits` for a 64-bit machine.

There is also `uint` which is known as `unsigned integer` also follows the above rule.

We can give explicit length with `int32`, or `uint32`

The list of (signed and unsigned) integer data types =&gt; `int8, int16, int32, int64, byte, uint8, uint16, uint32, uint64` with `byte` being the alias for `uint8` .

| Type | Size | Range |
| --- | --- | --- |
| int | Depends on the platform. | \-2147483648 to 2147483647 in 32-bit systems and |
| \-9223372036854775808 to 9223372036854775807 in 64-bit systems |  |  |
| int8 | 8-bit | \-128 to 127 |
| int16 | 16-bit | \-32768 to 32767 |
| int32 | 32-bit | \-2147483648 to 2147483647 |
| int64 | 64-bit | \-9223372036854775808 to 9223372036854775807 |
| uint | Depends on the platform. | 0 to 4294967295 in 32-bit systems and |
| 0 to 18446744073709551615 in 64-bit systems |  |  |
| uint8 | 8-bit | 0 to 255 |
| uint16 | 16-bit | 0 to 65535 |
| uint32 | 32-bit | 0 to 4294967295 |
| uint64 | 64-bit | 0 to 18446744073709551615 |

```go
var x uint = 32
var a int = -32
var b uint32 = 4222222
var c int64 = -9999999999
```

Float: The float data type is used to store numbers with decimal points. To represent floats we need to use `float32` and `float64` (there is no `float` type

| Type | Size | Range |
| --- | --- | --- |
| float32 | 32 bits | \-3.4e+38 to 3.4e+38. |
| float64 | 64 bits | \-1.7e+308 to +1.7e+308. |

```go
var a float32 = 123.65
var b float32 = 3.4e+38
var c float64 = 1.7e+308
```

Complex: Go has native support for complex numbers. Type `complex128` (64-bit real and imaginary parts) or type `complex64` (32-bit real and imaginary parts).

```go
c1 := complex(10, 11) // constructor init
c2 := 10 + 11i        // complex number init syntax
```

* **String**: represents a string value.
    
    ```go
    s := "Hello World !"
    var newString = "New World"
    var nextString string = "next Go"
    ```
    
    Strings in Go are a sequence of UTF-8 characters enclosed in double quotes (“). If you use the single quote (‘) you mean one character (encoded in UTF-8) — which is *not* a `string` in Go.
    

### Keywords:

Keywords are reserved words in every language. There are 25 keywords available in the Go language. These keywords were pre-defined inside the go language library and cannot be used as identifiers inside the go program. Keywords cannot be used as variable names or constants.

<table><tbody><tr><td colspan="1" rowspan="1"><p><code>break</code></p></td><td colspan="1" rowspan="1"><p><code>default</code></p></td><td colspan="1" rowspan="1"><p><code>func</code></p></td><td colspan="1" rowspan="1"><p><code>interface</code></p></td><td colspan="1" rowspan="1"><p><code>select</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>case</code></p></td><td colspan="1" rowspan="1"><p><code>defer</code></p></td><td colspan="1" rowspan="1"><p><code>go</code></p></td><td colspan="1" rowspan="1"><p><code>map</code></p></td><td colspan="1" rowspan="1"><p><code>struct</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>chan</code></p></td><td colspan="1" rowspan="1"><p><code>else</code></p></td><td colspan="1" rowspan="1"><p><code>goto</code></p></td><td colspan="1" rowspan="1"><p><code>package</code></p></td><td colspan="1" rowspan="1"><p><code>switch</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>const</code></p></td><td colspan="1" rowspan="1"><p><code>fallthrough</code></p></td><td colspan="1" rowspan="1"><p><code>if</code></p></td><td colspan="1" rowspan="1"><p><code>range</code></p></td><td colspan="1" rowspan="1"><p><code>type</code></p></td></tr><tr><td colspan="1" rowspan="1"><p><code>continue</code></p></td><td colspan="1" rowspan="1"><p><code>for</code></p></td><td colspan="1" rowspan="1"><p><code>import</code></p></td><td colspan="1" rowspan="1"><p><code>return</code></p></td><td colspan="1" rowspan="1"><p><code>var</code></p></td></tr></tbody></table>

### References:

[Learning GO book](https://www.miek.nl/go/#learning-go)

[Go variables by Tutorial point](https://www.tutorialspoint.com/go/go_variables.htm#:~:text=A%20variable%20is%20nothing%20but,be%20applied%20to%20the%20variable.)

[Identifiers and Keywords by coding ninjas](https://www.codingninjas.com/studio/library/identifiers-keywords#:~:text=There%20are%2025%20keywords%20available,a%20variable%20name%20or%20constant.)

[variables and constant in go](https://medium.com/swlh/variables-and-constants-in-go-42c5a3ca60c7)

[Difference between := and = operators in Go](https://stackoverflow.com/questions/17891226/difference-between-and-operators-in-go)

[Type inference in go](https://go.dev/tour/basics/14)

[Data types in Go](https://www.geeksforgeeks.org/data-types-in-go/)

[go multiple variable](https://www.w3schools.com/go/go_variable_multi.php)s

[Integer data types](https://www.w3schools.com/go/go_integer_data_type.php)

[complex numbers](https://www.tutorialandexample.com/complex-number-data-types-in-golang)