美文网首页
2019-10-16 Golang copy struct

2019-10-16 Golang copy struct

作者: Sea_Shore | 来源:发表于2019-10-17 07:03 被阅读0次

    The go is passing by value.
    That is, if I do
    a = b
    a is a copy of b.

    But if a and b are pointers.
    if I do a = b.
    it's pass by reference.
    to copy value,
    I need to do
    *a = *b

    package main
    
    import (
        "fmt"
    )
    
    type T struct {
        Id int
        Name string
    }
    
    func main() {
        fmt.Println("Hello, playground")
        
        // var t T
        t := &T{}
        t.Id = 3
        t.Name = "what"
        
        fmt.Printf("%v \n", t)
        
        // var t2 T
        t2 := &T{}
        
        *t2 = *t
        fmt.Printf("%v \n", t2) 
        
        t.Id =4
        fmt.Printf("%v \n", t)
        fmt.Printf("%v \n", t2) 
    }
    

    相关文章

      网友评论

          本文标题:2019-10-16 Golang copy struct

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