Go learning record

Summary of the append operation of Go slice : https://segmentfault.com/a/1190000020170529

 

1. The difference between array and slice

  1. When declaring an array, the length of the array or...,
  2. When declaring a slice, when the parenthesis is empty as a function parameter, what the array passes is a copy of the array, but what the slice passes is a pointer.

The array type array is a fixed-length array, the length of the array must be determined before use

Features of golang array:

  • Arrays in golang are value types, that is to say, if you assign an array to another array, then the entire array is actually copied. If the array in golang is used as a function parameter, then it is actually passed The parameter of is a copy of the array, and the pointer array, which is not an array, is also part of the Type. This means that [10]int and [20]int are not the same.
  • The slice type slice is a reference type, which is a dynamic pointer to the array slice. A slice is indeterminate and always points to the data structure of the underlying array array.

2、

<1>, two forms new(T)and &T{}full equivalent: both allocate a zero T and return a pointer to the allocated memory. The only difference is that &T {} does not work for builtin types like int ; you can only execute it new(int).

<2>, struct returns a structure, new returns a pointer

          1.new(T) creates an instance of type T without any data and returns a pointer to the instance;
         2.make(T, args) can only create slices, maps and channels, and returns an initial value args( Non-zero) instance of type T , non-pointer.

Guess you like

Origin blog.csdn.net/yu1336199790/article/details/109027651