Variables

Types

Go is a statically typed language. This means that variables are assigned a type when created and can only hold values of that type. This helps with avoiding runtime errors by catching them at compile-time

Go supports several types. The most common being

TypeDescriptionZero Value
intInteger value, can be 64 or 32 bits depending on the system it runs on0
boolBoolean valuefalse
stringString of UTF-8 characters""
sliceGrowable list of a typenil
mapKey-value data structurenil
structA collection of named attributes (Similar to objects in other languages)Empty struct
interfaceA type that holds a value with defined methodsnil
pointersA type that stores an address in memory of a variable not the variablenil
channelsA pipe (buffered or not) for sending data asynchronouslynil

Other types include more control over the size of the variables, as well as some particular use cases like complex numbers:

TypeDescriptionZero value
uintUnsigned 32 or 64 bit integer0
uint8Unsigned 8 bit integer (0-255)0
uint16Unsigned 16 bit integer (0-65535)0
uint32Unsigned 32 bit integer (0-4294967295)0
uint64Unsigned 64 bit integer (0-18446744073709551615)0
int8Signed 8 bit integer (-128-127)0
int16Signed 16 bit integer (-32768-32767)0
int32Signed 32 bit integer (-2147483648 - 2147483647)0
int64Signed 64 bit integer (-9223372036854775808 - 9223372036854775807)0
float3232 bit floating point (decimal) values0
float6464 bit floating point (decimal) values0
complex64Complex numbers with real and imaginary part float32(0+0i)
complex128Complex numbers with real and imaginary part float64(0+0i)
byteAlias for uint80
runeAlias for int320
uintptrUnsigned integer large enough to hold any pointer address (32 or 64 bit).0
arrayFix list of items[]
functionA function can be stored in a variable (functional programming)nil

Declaring variables

Go supports two types of variable declaration:

  1. Creation and assignment: This type can be used to declare variables at the package and function levels. If using just the creation statement (var i int) the variable will have the corresponding zero value
var i int // Create for a given type
i = 3 // Assign the value
var j = 3 // Infers the type
  1. Single statement: This can only be used in function levels
i := 3 // Creates and infers the type, then assigns the value
Redeclare variables

Once declared a value cannot be redeclared in the same scope, only a new value can be assigned, so the following is not valid

func main() {
    i := 3
    i := 5
}

Scope of variables

Depending on where they are declared variables can be “visible” to different scopes:

  • Package scoped: When declared at the package level, it can be seen by the entire package
package oe

var word = "hello"

func afunction(){}
  • Function scoped: Only visible inside a particular function
func afunction(){
    var i = 3
}

func another() {
    // i is not accessible
}
  • Statement scoped: Only visible for an statement (loop, if) inside a function
func af() {
    for i := 0; i < 10; i++ {
        // i is accessible
    }
    // i is not visible
}

Shadowing

Redeclaring variables in different scopes can affect the values that can be read. This is a very common bug know as variable shadowing. Below an example

package main

var word = "hello"

func main(){
    var word = "world"
    // In this scope word is world
}

func other(){
    // In here word is hello
}