Go language basic pointer and new

Pointers in the
Go language There is no pointer operation in the Go language. You only need to remember two symbols
&: take address
:
take a value based on the address, take the address operator & and take the value based on the address operator
, are a pair of complementary operations
1. fetch address operation for the variable, the variable can be obtained pointer
value of the pointer variable is a pointer 2.
3. the operation of the pointer variable value, the original value of the variable can be obtained pointer variable to point to
the new and the make
Go for reference types of languages When we use variables, we need to not only declare, but also allocate memory space, otherwise our value cannot be stored, and for value type variables, there is no need to allocate memory, because we have already allocated it when we declare Memory, to allocate memory, it leads to new and make

Make is also used to allocate memory, but different from new, it is only used to create memory for slice, map, and chan. And the type he returns is the three types themselves, not pointers, because the three types are already reference types, there is no need to return pointers

package main

import "fmt"
func main() {
    
    
    a := 1
    fmt.Println(&a)
    b := new(int)
    fmt.Printf("%v--%d\n", b, *b)
    slice := make([]int, 0)
    fmt.Printf("%v\n", slice)
}

Guess you like

Origin blog.csdn.net/weixin_44865158/article/details/114531019