1. Pointer types

Golang’s pointer is simpler than c and C ++ Pointers. It only uses Pointers to find elements and does not involve pointer operations.

The go pointer has two basic uses:

(1) & fetch address

(2) * Values by address

n1 := 20
n2 := &n1 //n2 gets the address of n1,n2 is an int pointer
n3 := *n2 //n3 retrieves the value of n2
Copy the code

The address-fetch operator & and the value operator * are complementary operators. The & fetches the address, and * fetches the value to which the address points.

The interrelations and characteristics of variable, pointer address, pointer variable, address fetch, and value are as follows:

  • To obtain a pointer to a variable, perform an address fetch (&) operation.
  • The value of a pointer variable is a pointer address.
  • The value (*) operation on a pointer variable can obtain the value of the original variable pointed to by the pointer variable.

As with C/C ++, this is not repeated.

2. Similarities and differences between new method and make method

In Go, for reference types, such as pointer and slice, memory must be allocated for them after declaration, otherwise there is no way to assign values to pointer or slice variables. In this case, pointer and slice stores null values, namely nil. Value types, such as int, String, etc., are declared with memory allocated by default.

package  main
import "fmt"
 
func main(a){
	var a *int
	fmt.Println(a == nil)

	var slice []int
	fmt.Println(slice == nil)}Copy the code

Output result:

true
true
Copy the code

So you need to allocate memory for them with new and make, which are built-in functions in Go to allocate memory.

2.1 the new ()

(1) Basic usage

func new(T)// Return a *T 
Copy the code

T: indicates the type. The new function takes the type as an argument and returns a pointer to it

Such as:

package  main
import "fmt"
 
func main(a){
	var a = new(int) //a is an int pointer
	*a = 20
	fmt.Printf("The type of a is: %T\n%v\n",a,*a)
}
Copy the code

Output result:

The type of a is: *int 20Copy the code

(2) differences between new in Go and new in c++

New () in Go differs from new in c++ in that it does not initialize memory, it just sets it to zero.

A = new(T) assigns a the zeroed storage and returns its address, a value of type *T.

2.2 make ()

(1) Basic usage

Make is also used for memory allocation. Unlike new, make is used for memory creation of slice, map, and Chan (these are all datatypes unique to Go and are reference types, so there is no need to return Pointers to them) and returns an initialized (not zero) value of type T (not *T).

//make( []T, size, cap )
// Create an orthopedic slice of length 6 and capacity 10
a1 := make([]int.6.10) 

hash := make(map[int]bool.10)

ch := make(chan int.5)
Copy the code
  • T: type of element in the slice
  • Size: Number of slices accessed elements (i.e. length), obtained by len(a1)
  • Cap: The number of elements (i.e., capacity) that slices are allowed to grow to, obtained by Cap (A1)
  • A2 := make([]int,5), size = 5,cap = 5

2.3 Main differences between new() and make(

  • Make () initializes slice, map, and chan;
  • New () allocates an area of memory based on the type passed in and returns a pointer to that area