Skip to content

Commit cb8055c

Browse files
xljxlj
authored andcommitted
add doc for pointer
1 parent a343cfb commit cb8055c

5 files changed

Lines changed: 76 additions & 1 deletion

File tree

120-Pointers/pointer-1.gop

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// A pointer is a variable whose value is a memory address. Pointers are defined using an ampersand (the & character), known as the address operator, followed by
2+
// the name of a variable
3+
//
4+
// A pointer’s type is fixed, which means that when you create a pointer
5+
// to an int, for example, you change the value that it points to, but you can’t use it to point to the memory
6+
// address used to store a different type, such as a float64. This restriction is important, pointers are
7+
// not just memory addresses but, rather, memory addresses that may store a specific type of value.
8+
//
9+
// The type of a pointer is based on the type of the variable from which it is created, prefixed with an
10+
// asterisk (the * character). The type of variable named second is *int, because it was created by applying
11+
// the address operator to the first variable, whose value is int. When you see the type *int, you know it is a
12+
// variable whose value is a memory address that stores an int variable.
13+
//
14+
15+
first := 100
16+
var second *int = &first
17+
first++
18+
println("First:", first)
19+
println("Second:", second)

120-Pointers/pointer-2.gop

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Following a Pointer
2+
// The phrase following a pointer means reading the value at the memory address that the pointer refers
3+
// to, and it is done using an asterisk (the * character). The asterisk tells Go+ to follow the pointer and get the value at the memory location. This is known as dereferencing the pointer.
4+
5+
first := 100
6+
second := &first
7+
first++
8+
*second++
9+
var myNewPointer *int
10+
myNewPointer = second
11+
*myNewPointer++
12+
println("First:", first)
13+
println("Second:", *second)
14+
15+
// The first new statement defines a new variable, which I have done with the var keyword to emphasize
16+
// that the variable type is *int, meaning a pointer to an int value. The next statement assigns the value of
17+
// the second variable to the new variable, meaning that the values of both second and myNewPointer are the
18+
// memory location of the first value. Following either pointer accesses the same memory location, which
19+
// means incrementing myNewPointer affects the value obtained by following the second pointer.
20+
//
21+
// A common misconception is that the first and second variables have the same value, but that’s not
22+
// what is happening. There are two values. There is an int value that can be accessed using the variable
23+
// named first. There is also an *int value that stores the memory location of the first value. The *int value
24+
// can be followed, which will access the stored int value. But, because the *int value is, well, a value, it can be
25+
// used in its own right, which means that it can be assigned to other variables, used as an argument to invoke a
26+
// function, and so on.

120-Pointers/pointer-3.gop

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Understanding Pointer Zero Values
2+
// Pointers that are defined but not assigned a value have the zero-value nil.
3+
//
4+
// The pointer second is defined but not initialized with a value and is written out using the println
5+
// function. The address operator is used to create a pointer to the first variable, and the value of second is
6+
// written out again.
7+
//
8+
// A runtime error will occur if you follow a pointer that has not been assigned a value
9+
10+
first := 100
11+
var second *int
12+
println(second)
13+
second = &first
14+
println(second)

120-Pointers/pointer-4.gop

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Pointing at Pointers
2+
// Given that pointers store memory locations, it is possible to create a pointer whose value is the memory
3+
// address of another pointer.
4+
5+
first := 100
6+
second := &first
7+
third := &second
8+
println(first)
9+
println(*second)
10+
println(**third)
11+
12+
// The syntax for following chains of pointers can be awkward. In this case, two asterisks are required.
13+
// The first asterisk follows the pointer to the memory location to get the value stored by the variable named
14+
// second, which is an *int value. The second asterisk follows the pointer named second, which gives access
15+
// to the memory location of the value stored by the first variable. This isn’t something you will need to do
16+
// in most projects, but it does provide a nice confirmation of how pointers work and how you can follow the
17+
// chain to get to the data value.

120-Pointers/pointer.gop

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)