Functions in the Go language can return multiple values, which differs greatly from other programming languages. For those with other language programming experience, the biggest obstacle is not learning this feature, but it is hard to think about using it.
Simple as an example of exchanging two values:
" FMT " int int) (intint) {return B, A} func main () {a: /c11>1B:2= Swap (A, b) fmt. Println (a) fmt. Println (b)}
The output is:
2
1
If in other languages, our first thought must be to create an intermediate variable for Exchange. But it is easy to use the function of returning multiple values in go.
In addition, if the declared variable is not used in go, the compilation cannot pass. So if the value returned by the multiple return value function is not required, you can use an underscore to receive the value, indicating that the value is discarded, as in the above example, only the first value that needs to be returned can be:
A, _ = Swap (A, B)
Go Language Example-function returns multiple values