Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One-liner for creating struct with blank fields?

Tags:

go

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?

like image 970
Michael Deardeuff Avatar asked Dec 05 '25 12:12

Michael Deardeuff


1 Answers

You could specify the fields

f := Foo{a: 1, b: 2, c: 3}
fmt.Println(f) //{1 2 3 0}
like image 181
dave Avatar answered Dec 08 '25 16:12

dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!