SinceNow.net

指针

Go语言中的指针

phpangel   2022-07-07 13:22:49

做个备忘,随时查看

package main

import "fmt"

var s string
var ps *string

func hello(s *string) {
    *s = "654321"
    fmt.Printf("hello 内存地址:%p\n", &s)
    fmt.Printf("hello 的值是:%s\n", *s)
}

func main() {
    s = "123456"
    ps = &s
    fmt.Printf("main 内存地址:%p\n", &s)
    fmt.Printf("main 内存地址:%p\n", ps)
    fmt.Printf("main s的值是:%s\n", s)
    fmt.Printf("main ps的值是:%s\n", *ps)
    hello(&s)
    fmt.Printf("main 修改后ps的值是:%s\n", *ps)
    fmt.Printf("main 修改后s的值是:%s\n", s)
}

运行结果

main 内存地址:0x8dc270
main 内存地址:0x8dc270
main s的值是:123456
main ps的值是:123456
hello 内存地址:0xc000006030
hello 的值是:654321
main 修改后ps的值是:654321
main 修改后s的值是:654321

指针

Related Articles

0 Comments

message
沪ICP备2024072411号 © 2022 SinceNow.net - GitHub
Login