Different from the C / C ++ in the pointer, the pointer Go language can not be shifted and operation are safe pointer.
Go wants to understand the language of pointers you need to know three concepts: pointer address, pointer types, and pointer values .
concept
After any program data loaded into memory, the memory has their address, which is a pointer. In order to address data stored in a memory, we need a pointer variable.
For example, "Life is short, Let's go" phrase, I want it written into the program, a program started this sentence is to be loaded into memory (assuming that memory address 0x123456), I put these words assigned to the program variable A
, the memory address assigned to the variable B
. This time variable B
is a pointer variable. By variables A
and variables B
can be found in this sentence.
Go language pointer arithmetic can not be offset and thus pointer operation Go language is very simple, we only need to remember two symbols:
&
(Address), and *
(the address value) .
And pointer type pointer address
Each variable at run time has an address, the address represents the variable location in memory.
Using the Go language &
character variables placed in front of the variable "fetch address" operation.
Value type (int, float, bool, string , array, struct) Go language has a corresponding pointer type, such as: *int
, *int64
, *string
and the like.
Take variable pointer syntax:
PTR: V = & // V of type T
- v: is taken to be representative of the address of the variable, the type of
T
- ptr: receiving a variable address, it is PTR type
*T
, pointer type T is called. * Represents the pointer.
eg:
func main() { a := 10 b := &a fmt.Printf("a:%d ptr:%p\n", a, &a) // a:10 ptr:0xc00001a078 fmt.Printf("b:%p type:%T\n", b, b) // b:0xc00001a078 type:*int fmt.Println(&b) // 0xc00000e018 }
Let's look at b := &a
an illustration:
Pointer Value
After using the & operator takes the address pointer will get one of ordinary variable this variable can then be used for the operation * pointer, i.e. the pointer value, as follows.
main FUNC () { // pointer value a: = 10 b : a = & // address of the second variable a, b, save the pointer to the FMT. the Printf ( "type of b:% T \ n-", b) C : = B * // pointer value (according to the pointer to the memory value) FMT. the Printf ( "type of C:% T \ n-", C) fmt.Printf("value of c:%v\n", c) }
result:
type of b:*int type of c:int value of c:10
Summary: address-operator &
and operators values *
is a pair of complementary operator, &
fetch address, *
the address pointed to by the address value extracted.
- Taken to address variable (&) operation can be obtained pointer variable this variable.
- Value of the pointer variable is a pointer address.
- Pointer variable value (*) operation values can be obtained pointer variable to point to the original variables
Example value pointer pass:
func test(a1 [3]int) { a1 [ 0] = 100 } // receiving a pointer value of the pointer FUNC test1 (A1 * [. 3 ] int) { // only to a pointer type * Value Method A1 [ 0] = 100 } func main() { a := [3]int{1,2,3} Test (A) // copy function in the namespace of the array and assigned to the variable A1 fmt.Println (A) // [2. 3. 1] test1 (& A) // shaped test1 is a function of the received reference pointer, need to pass a pointer fmt.Println (a) // [100 2. 3] is the same as a pointer address to get }
new
Function is obtained using the new type of a pointer, and the value of the zero value corresponding to the type of the pointer.
to give new pointer value for initialization type
signature
func new(Type) *Type
- Type indicates the type, new function accepts a parameter, which is a type
- * Type indicates the type of pointer, new function returns a pointer to the memory address type
main FUNC () { var A = new new (int) // get a pointer of type int fmt.Println (A) // 0xc000054058 * A = 10 // a pointer assignment fmt.Println (A) // 0xc000054058 fmt.Println (* A) // 10 where c = new ([3 ] int) fmt.Println(c) // &[0 0 0] // (*c)[0] = 10 c[0] = 10 fmt.Println(*c) // [10 0 0] }
Note: var a *int
just declare a pointer to a variable but not initialized, will have a memory as the pointer reference types need to be initialized before they can be assigned to it
make
also make for memory allocation, different from new, only used slice, map and memory chan created, and type it returns is three type itself, rather than their pointer type, because these three types is reference types, so there is no need to return to their hands up. Function signature make the following functions:
func make(t Type, size ...IntegerType) Type
make function is irreplaceable, we use the slice, map and channel, they both need to make to initialize before you can operate on them. This we have explained in the previous chapter, we will explain in detail about the channel in subsequent chapters.
The examples in this section beginning in var b map[string]int
just declare a variable b is a variable of type map, and then need something like the following sample code as make use initialization function can be assigned key-value pairs:
func main() { var b map[string]int b = make(map[string]int, 10) B [ "bears two"] = 100 FMT . Println (B) }
New and make a difference
- Both are used for memory allocation.
- make only for initializing slice, map and channel, or the return of the three types of reference itself;
- And for the new type of memory allocation, memory type and a value corresponding to the zero value of the return pointer is a pointer type.