Go language - pointer | new | make

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 Bis a pointer variable. By variables Aand variables Bcan 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, *stringand 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 ofT
  • 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 := &aan 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 *intjust 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]intjust 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

  1. Both are used for memory allocation.
  2. make only for initializing slice, map and channel, or the return of the three types of reference itself;
  3. 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.

Guess you like

Origin www.cnblogs.com/waller/p/11954121.html