如何在Golang

时间:2016-10-27 07:14:41

标签: go

我有一个由goa生成的结构

type a_weird_struct struct{
 a *string
 b *string
 c *struct{
  d *int
  e *int
  f *int
 }
}

分配给此结构的正确方法是什么。具体来说,如何初始化结构点" c"。

2 个答案:

答案 0 :(得分:1)

尝试像这样启动指针

func initPointer() {
    astr, bstr := "xxxx", "yyyy"
    dint, eint, fint := 1, 2, 3
    x := &a_weird_struct{
        a: &astr,
        b: &bstr,
        c: &(struct {
            d *int
            e *int
            f *int
        }{
            d: &dint,
            e: &eint,
            f: &fint,
        }),
    }
     fmt.Println(x)
}

答案 1 :(得分:0)

添加一个小助手来进行转换。

func Int(v int) *int {
    return &v
}

这避免了临时。