go 指针

作者: wyrover | 来源:发表于2018-06-22 21:55 被阅读24次
    package main
    
    import (
        "fmt"
        "unsafe"
    )
    
    func main() {
        var value int64 = 5
        var p1 = &value
        var p2 = (*int32)(unsafe.Pointer(p1))
    
        fmt.Println("*p1: ", *p1)
        fmt.Println("*p2: ", *p2)
        *p1 = 5434123412312431212
        fmt.Println(value)
        fmt.Println("*p2: ", *p2)
        *p1 = 54341234
        fmt.Println(value)
        fmt.Println("*p2: ", *p2)
    }
    
    

    返回指针

    package main
    
    import (
        "fmt"
    )
    
    func returnPtr(x int) *int {
        y := x * x
        return &y
    }
    
    func main() {
        sq := returnPtr(10)
        fmt.Println("sq:", *sq)
    
        fmt.Println("sq:", sq)
    }
    
    

    相关文章

      网友评论

          本文标题:go 指针

          本文链接:https://www.haomeiwen.com/subject/jaulyftx.html