~kotrunga/a-tour-of-go

ref: b10fb217bb6c0e0a4214a8d1e71e504481c0a13e a-tour-of-go/basics/structs-slices-maps/1.3.1_pointers.go -rw-r--r-- 528 bytes View raw
b10fb217kotrunga Update incorrect comments on 1.3.10 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
	Notes 1.3.1

	- Pointers hold the memory address of a value
	- Zero value is nil
	- The & operator gives the address of the variable
	- * dereferences the pointer
	- Go has no pointer arithmetic
*/

package main

import "fmt"

func main() {
	i, j := 42, 2701

	p := &i	// point to i
	fmt.Println(*p)	// read i through the pointer p
	*p = 21	// set i through pointer p
	fmt.Println(i)	// see the new value of i

	p = &j	// point p to j
	*p = *p / 37	// divide j through the pointer
	fmt.Println(j)	// see the new value of j
}