Golang值传递和指针传递

package main

import (
    "fmt"
)

func swap1(x, y, p *int) {
    if *x > *y {
        *x, *y = *y, *x
    }
    *p = *x * *y
}

func swap2(x, y int) (int, int, int) {
    if x > y {
        x, y = y, x
    }
    return x, y, x * y
}

func main() {
    i := 9
    j := 5
    product := 0
    swap1(&i, &j, &product)
    fmt.Println(i, j, product)    //5 9 45

    a := 64
    b := 23
    a, b, p := swap2(a, b)
    fmt.Println(a, b, p)   //23 64 1472
}

----------------------------------【喜欢打赏】-------------------------------------------

小主,辛苦啦!文章棒棒哒,赏杯咖啡吧...
Golang值传递和指针传递

----------------------------------【喜欢打赏】-------------------------------------------

相关文章:

  • 2021-06-26
  • 2021-06-05
  • 2021-10-01
  • 2021-12-13
猜你喜欢
  • 2021-06-03
  • 2021-11-09
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2021-05-23
相关资源
相似解决方案