Variables

Variable declaration

var intNum int // (variable declaration) (name) (type)

Variable types

TypesDetailsDefault value
int / int8 / int16 / int32 / int64Indicate how many bits we want to allocate to store the integer0
uint / uint8 / uint16 / uint32 / uint64Unsigned integers to only store positive values, can store twice the amount of integers0
float32 / float64-0
stringUse 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
""
runeUse 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"
  • const means declaring a variable that we cannot change after its created, and must be initialised with value