Home>Article>Backend Development> Detailed explanation of Golang array transfer

Detailed explanation of Golang array transfer

藏色散人
藏色散人 forward
2020-11-23 14:59:36 3877browse

The following columngolang tutorialwill introduce to you the passing ofGolangarrays. I hope it will be helpful to friends in need!

Detailed explanation of Golang array transfer

Detailed explanation of the transfer of Golang arrays

Today I will share with you a detailed explanation of the transfer of Golang arrays. I think the content is quite good, and I would like to share it with you now. It has a very good reference value. If you need it, please follow the editor and take a look.

Concept introduction

Arrays and Slices

An array is a numbered and fixed-length sequence of data items of the same unique type. The array length is up to 2Gb and it is a value type. A slice is a reference to a contiguous fragment of an array, so a slice is a reference type.

Pass by value and pass by reference

There are two ways to pass parameters of functions in Go language, pass by value and pass by reference. Go uses pass-by-value to pass parameters by default, that is, passing a copy of the parameter. When the value of the copy is changed in the function, the original variable will not be affected.

Passing by reference can also be called "passing by value", but the copy is a copy of an address, through which the value at the address pointed to by this value can be modified.

In the Go language, when calling functions, reference types (slice, map, interface, channel) are passed by reference by default.

Disadvantages of array passing

Generally, passing a pointer costs less than passing a copy, especially when the array is particularly large. The specific reason is:

Value transfer requires a complete copy of the initial array and puts this copy on the stack, which will consume a lot of running time, so the efficiency of the value transfer method is relatively low.

The copy of the initial array requires additional memory space (memory on the stack)

The compiler needs to specifically generate a part of the code to copy the initial array, which will make the program larger.

How to avoid

As introduced above, there are two methods. The first one uses pointers, that is, passing by reference; the second one uses slices, because slices are references. Type, passed by reference by default.

Use pointers to pass

package main import "fmt" func main() { var arr = [5]int{1, 2, 3, 4, 5} fmt.Println(sum(&arr)) } func sum(arr *[5]int) int { s := 0 for i := 0; i < len(arr); i++ { s += arr[i] } return s }

Use slices to pass

package main import "fmt" func main() { var arr = [5]int{1, 2, 3, 4, 5} fmt.Println(sum(arr[:])) } func sum(arr []int) int { s := 0 for i := 0; i < len(arr); i++ { s += arr[i] } return s }

The last method is usually more common.

The above is the detailed content of Detailed explanation of Golang array transfer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:zzvips.com. If there is any infringement, please contact admin@php.cn delete