Tutorial by Examples

Go supports pointers, allowing you to pass references to values and records within your program. package main import "fmt" // We'll show how pointers work in contrast to values with // 2 functions: `zeroval` and `zeroptr`. `zeroval` has an // `int` parameter, so arguments will be ...
Pointer Methods Pointer methods can be called even if the variable is itself not a pointer. According to the Go Spec, . . . a reference to a non-interface method with a pointer receiver using an addressable value will automatically take the address of that value: t.Mp is equivalent to (&t)....
Pointers can be dereferenced by adding an asterisk * before a pointer. package main import ( "fmt" ) type Person struct { Name string } func main() { c := new(Person) // returns pointer c.Name = "Catherine" fmt.Println(c.Name) // prints: Cathe...
Slices are pointers to arrays, with the length of the segment, and its capacity. They behave as pointers, and assigning their value to another slice, will assign the memory address. To copy a slice value to another, use the built-in copy function: func copy(dst, src []Type) int (returns the amount o...
func swap(x, y *int) { *x, *y = *y, *x } func main() { x := int(1) y := int(2) // variable addresses swap(&x, &y) fmt.Println(x, y) }

Page 1 of 1