Difference between array and slice in go language

&The meaning of the symbol is to take the address of the variable, such as: athe address of the variable is&a 

*The meaning of the symbol is to take the value of the pointer, such as: *&a, which is athe value of the address where the variable is located, of course, athe value of

*and  & can cancel each other, at the same time, note that it *&can be canceled, &*but  not canceled

First things first: unbuffered = blocked, buffered = non-blocked

for example

c1:=make(chan int) unbuffered

c2:=make(chan int,1) is buffered

c1<-1                            

Unbuffered is not only to put 1 to the c1 channel, but to have other Ctrip<-c1 take over this parameter, then c1<-1 will continue, otherwise it will be blocked all the time

And c2<-1 will not block, because the buffer size is 1. Only when the second value is put in the first one has not been taken away, it will block at this time.

make an analogy

The unbuffered one is a messenger who goes to your door to deliver the letter. If you are not at home, he will not leave. You must accept the letter before he will leave.

The unbuffered guarantee letter can reach you

The buffer is that a messenger who goes to your house still arrives at your mailbox and turns around and walks away, unless your mailbox is full and he has to wait for the mailbox to be empty.

A buffered letter of assurance will go into your home mailbox

The article is referenced from: https://studygolang.com/articles/152

The Comma-ok assertion syntax in the Go language can directly determine whether it is a variable of this type: value, ok = element.(T), where value is the value of the variable, ok is a bool type, element is an interface variable, and T is an assertion type. If element does store a value of type T, then ok returns true, otherwise it returns false.

Difference between array and slice in go language

One sentence summary: slices are dynamic arrays, pay attention to the difference between initialization and function parameters

A slice is a variable-length, fixed-capacity sequence of identical elements. A slice in Go is essentially an array. The capacity is fixed because the length of the array is fixed, and the capacity of the slice is the length of the hidden array. Variable length means that it is variable within the range of the length of the array.

1. Initialization: The size of the array needs to be specified. If it is not specified, the size will be automatically calculated according to the initialization. It cannot be changed. The slice does not need to specify the size . 

2. Function transfer: The size of the array needs to be explicitly specified, and the slice does not need it. Arrays are passed by value, slices are passed by address

The article is referenced from: http://blog.csdn.net/Nick_666/article/details/78640870

pass-by-address and pass-by-value

Pass-by-value is just a copy of the content of the variable. The function operates on another variable, but the value of the other variable is the same as the value of the passed variable .

The address transfer is to directly pass the address of the variable to the function , then the function directly operates on the original variable. So the value will change.

The article is referenced from: https://zhidao.baidu.com/question/348527211.html

The main differences between new and make in golang are as follows:

1. make can only be used to allocate and initialize data of type slice, map, chan; new can allocate data of any type

2. The new assignment returns a pointer, that is, the type *T; make returns a reference, that is, T;

3. The space allocated by new is cleared. After make allocation, it will be initialized.

( Essentially, what makes the three built-in types different is that references must be initialized before they can be used, and make initializes the internal data structures, filling them with appropriate values. make returns the initialized (non-zero) value . )



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325972329&siteId=291194637