[Golang] 指標

*int : 宣告的指標整數

&變數名 : 拿記憶體位置

fmt.Printf: 格式化輸出

package main

import "fmt"


func main(){
    var a int = 100
    var s string = "hello"

    fmt.Print(a)
    fmt.Print(s)
    fmt.Println("%p %p", &a, &s)

    var address ="Bocky address"

    ptr := &address
    fmt.Printf("Address =%p \n", ptr) //Address =0xc000010260

    value := *ptr
    fmt.Printf("value Type: %T \n", value)  // value Type: string
    fmt.Printf("value value: %s \n", value) //value value: Bocky address

    aa, bb := 10, 100
    swap(&aa, &bb)
    fmt.Println(aa,bb)
}



func swap(m, n *int){

    // s =10
    s := *m

    // m 100  n 100
    *m = *n

    // n 10
    *n = s
}

發佈留言