Variables
Examples in
Go
In Go, variables are explicitly declared and used by the compiler to e.g. check type-correctness of function calls.
package main
import "fmt"
func main() {
// var declares 1 or more variables.
var a = "initial"
fmt.Println(a)
// You can declare multiple variables at once.
var b, c int = 1, 2
fmt.Println(b, c)
// Go will infer the type of initialized variables.
var d = true
fmt.Println(d)
// Variables declared without a corresponding initialization are zero-valued.
// For example, the zero value for an int is 0.
var e int
fmt.Println(e)
// The := syntax is shorthand for declaring and initializing a variable.
f := "apple"
fmt.Println(f)
}
A pointer is a programming language object that stores the memory address of another value located in computer memory.
package main
import "fmt"
func zeroval(ival int) {
ival = 0
}
func zeroptr(iptr *int) {
*iptr = 0
}
func main() {
i := 1
fmt.Println("initial:", i)
zeroval(i)
fmt.Println("zeroval:", i)
zeroptr(&i)
fmt.Println("zeroptr:", i)
fmt.Println("pointer:", &i)
}