美文网首页
golang 语法

golang 语法

作者: iamsea | 来源:发表于2023-03-24 11:26 被阅读0次

    语法易错点

    • 传结构的值时,结构体的对象类型会被转换为接收者定义的类型
        dataList := []struct {
            dst  interface{}
            src  interface{}
            want interface{}
        }{
            //{PersonM{Name: "Alice", Age: 20}, PersonM{Age: 20, Name: "Alice"}, PersonM{Name: "Alice", Age: 20}},
            //{PersonM{Name: "Alice", Age: 20}, PersonM{Name: "Bob", Age: 20}, PersonM{Name: "Bob", Age: 20}},
            {&PersonM{Name: "Alice", Age: 20}, PersonM{Name: "Bob", Age: 21}, PersonM{Name: "Bob", Age: 21}},
            //{WomanM{Name: "Alice", Age: 20}, PersonM{Name: "Alice", Age: 20}, WomanM{Name: "Alice", Age: 20}},
            //{WomanM{Name: "Alice", Age: 20}, PersonM{Name: "Alice", Age: 20, Height: 170}, WomanM{Name: "Alice", Age: 20}},
            //{PersonM{Name: "Alice", Age: 20, Height: 170}, WomanM{Name: "Alice", Age: 21}, PersonM{Name: "Alice", Age: 21, Height: 170}},
            //{PersonM{Name: "Alice"}, WomanM{Name: "Bob", Age: 21}, PersonM{Name: "Bob", Age: 21}},
        }
    
    • 值在使用时要初始化,不然会报访问nil对象
      runtime error: invalid memory address or nil pointer dereference\ngoroutine

    相关文章

      网友评论

          本文标题:golang 语法

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