Golang passing arrays to the function and modifying it

后端 未结 2 778
遇见更好的自我
遇见更好的自我 2021-01-21 14:47

In most languages (like c++) passing arrays result in implicitly passing it by a reference, so any changes to the passed array in the function will result in changing the origin

相关标签:
2条回答
  • 2021-01-21 15:45

    The explanation is rather simple: there isn't a single array declared or used explicitly in your code above. Your tab local variable and the tab parameter are slices.

    In Go the length of an array is part of the type, e.g. [3]int (this is true to an extent that for example [2]int and [3]int are 2 different / distinct array types). If the length is not present (either explicit like [2]int or implicit like in the composite literal [...]int{1, 2, 3}), then that is not an array type but a slice type.

    Yes, as you read, an array value means all its elements, and when passed around (or assigned), all its elements are copied. Slices however are just small descriptors, headers, describing a contiguous section of an array; and when slices are passed around (or assigned), only this header is copied (the pointer included), which will point to the same underlying array. And so if you modify the elements of the slice-copy, the changes will be reflected in the original slice as there is only one backing array that holds the elements.

    If you want to know what's in a slice header exactly, you may check out the reflect.SliceHeader type: it's a struct containing the pointer to the first element of the slice, the length and the capacity of the slice.

    Please read the following blog posts which explain this in great details:

    Go Slices: usage and internals

    Arrays, slices (and strings): The mechanics of 'append'

    Also see these related questions for more details:

    Why have arrays in Go?

    Are golang slices pass by value?

    0 讨论(0)
  • 2021-01-21 15:45

    What you are defining is not array but a slice of an array which is passed by reference as specified in golang documentation. Check this link.

    0 讨论(0)
提交回复
热议问题