[Golang basic summary] compare arrays and slices

1, capacity is scalable. Non-scalable array's capacity, the capacity of retractable sections.

2, can be compared. For an array of the same kind and the same length dimension can be compared, only the sections compared with nil.

3, as parameters, an array is generally passed pointer to an array, but may be a function of the slice as a parameter, not to say that the array can not be used as a reference, just to save money.

Pass an array between a function is expensive operation. When the transfer function between the variables, is always passed by value. If the entire variable is an array, means that the entire array, no matter how long, will be reproduced in full, and passed to the function. Therefore, a better and more effective way is just passed a pointer to the array, so that only eight bytes of data to copy.

In the 64-bit architecture machine, a slice needs to 24 bytes of memory: a pointer field requires 8 bytes, the length and capacity required fields are 8 bytes. Since the data contained in the sections associated with the underlying array, it does not belong to the slice itself, so to copy the slices when the arbitrary function, will not affect the size of the underlying array. When you copy a copy only a slice itself, it does not involve the underlying array.

Guess you like

Origin www.cnblogs.com/LydiammZuo/p/11877289.html