douzhajie7168 2015-02-17 22:03
浏览 32
已采纳

使用空字段创建结构的一线工具?

I have a struct with blank fields:

type Foo struct {
    a uint32
    b uint32
    c uint32
    _ uint32 //padding
}

For structs without blank fields I enjoy using one-liner initialization. But, I cannot seem to do this for types with blank fields:

Foo{1,2,3}   // too few values in struct initializer
Foo{1,2,3,0} // cannot refer to blank field or method
Foo{1,2,3,_} // cannot use _ as value

To keep the nice syntax, must I name the unused field?

  • 写回答

1条回答 默认 最新

  • donglu1913 2015-02-17 22:06
    关注

    You could specify the fields

    f := Foo{a: 1, b: 2, c: 3}
    fmt.Println(f) //{1 2 3 0}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?