1. package main
    2. import "fmt"
    3. func main () {
    4. var i, j int = 1, 2 //并行赋值
    5. fmt.Println(i, j)
    6. p := &i
    7. h := &j
    8. swap(p, h)
    9. fmt.Println(i, j)
    10. }
    11. func swap(num1 *int, num2 *int) {
    12. temp := *num1 //值交换,而不是指针交换
    13. *num1 = *num2
    14. *num2 = temp
    15. }
    1. 1 2
    2. 2 1