Variables
Variable declaration
var intNum int // (variable declaration) (name) (type)Variable types
| Types | Details | Default value |
|---|---|---|
| int / int8 / int16 / int32 / int64 | Indicate how many bits we want to allocate to store the integer | 0 |
| uint / uint8 / uint16 / uint32 / uint64 | Unsigned integers to only store positive values, can store twice the amount of integers | 0 |
| float32 / float64 | - | 0 |
| string | Use double quotes or backquotes len("") gives 2 because GO uses utf-8 encoding, so we can use the import ‘unicode/utf8’ and use the utf8.RuneCountInString to count the length | "" |
| rune | Use single quotes | ” |
| bool | - | false |
- We cannot do mathematical operations on different types, such as adding int to float32 is not allowed, though we can type cast one of them
- Division is rounded down
- We can also use type inference instead of declaring the type,
var myVar = "text" - Declaration and assignment:
myVar := "text" constmeans declaring a variable that we cannot change after its created, and must be initialised with value