package main import "fmt" //gmt.Pringln() //go函数可以返回多个值 func test() (a, b, c int) { return 1, 2, 3 } func main() { //多重赋值 a, b := 10, 20 //交换2个变量的值 var tmp int tmp = a a = b b = tmp fmt.Printf("a=%d, b=%d\n", a, b) //更简单的交换方式 a, b = b, a fmt.Printf("a=%d, b=%d\n", a, b) a = 10 b = 20 //匿名变量,丢弃数据不处理,_匿名函数需要配合函数返回值使用,才有优势 tmp, _ = a, b fmt.Printf("tmp=%d\n", tmp) var c, d, e int c, d, e = test() //返回1, 2, 3 fmt.Printf("c=%d, d=%d, e=%d\n", c, d, e) _, d, _ = test() //返回1, 2, 3 fmt.Printf("d=%d\n", d) }
原文地址:https://www.cnblogs.com/zyqy/p/11239632.html
时间: 2024-10-31 02:51:28