方式一:
package main
import("fmt")
func swap(a *int, b *int) {tmp := *a*a = *b*b = tmp}
func main() {a := 3b := 4fmt.Println(a, b) // 3, 4swap(&a, &b)fmt.Println(a, b) // 4, 3}
方式二
package main
import("fmt")
func swap(a int, b int) (int, int) {return b, a}
func main() {a := 3b := 4fmt.Println(a, b) // 3, 4a, b = swap(a, b)fmt.Println(a, b) // 4, 3}
方式三
package main
import("fmt")
func main() {a := 3b := 4fmt.Println(a, b) // 3, 4a, b = b, afmt.Println(a, b) // 4, 3}