Golang值傳遞和指針傳遞


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
}

----------------------------------【喜歡打賞】-------------------------------------------

小主,辛苦啦!文章棒棒噠,賞杯咖啡吧...
打賞

----------------------------------【喜歡打賞】-------------------------------------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM