Named or unnamed struct initialisation? What is best practice?

371 views
Skip to first unread message

snmed

unread,
Jul 5, 2017, 1:17:22 AM7/5/17
to golang-nuts
Hi gophers

I've a litte question about best practice for struct initialisation.  For example one can initialise structs like:
type A struct {
   name      string
   firstName string
}

a1 := A{"Mueller", "Max"}
a2 := A{name: "Mueller", firstName: "Max"}

The first one is concise and intuitive, but the second is more robust in case of refactoring.
If anyone adds a string field at the beginning of the struct, it compiles fine but changes the meaning of all initialised structs in the project, if nobody 
pays attention to that.

Is there any best practice or advises how structs should be initialised and/or refactored?

Cheers

Tamás Gulácsi

unread,
Jul 5, 2017, 1:41:04 AM7/5/17
to golang-nuts
Named.

snmed

unread,
Jul 5, 2017, 2:02:19 AM7/5/17
to golang-nuts
Okay thanks, as I supposed. Personally i used always named initialisation despite of the overhead, but sometimes I feel the urge to use the short unamed initialisation. ;-)

Am Mittwoch, 5. Juli 2017 07:41:04 UTC+2 schrieb Tamás Gulácsi:
Named.

r...@skyportsystems.com

unread,
Jul 5, 2017, 8:26:27 PM7/5/17
to golang-nuts
for some of our sensitive structures that are tricky and have multiple fields of the same type, there is a trick you can use:

type bar struct {
   A int
   _ bool
}



Dan Kortschak

unread,
Jul 5, 2017, 11:45:30 PM7/5/17
to r...@skyportsystems.com, golang-nuts
On Wed, 2017-07-05 at 17:26 -0700, rsr via golang-nuts wrote:
> type bar struct {
>    A int
>    _ bool
> }


or `type bar struct { A int; _ [0]byte }` to avoid the additional byte use.

snmed

unread,
Jul 6, 2017, 1:44:24 AM7/6/17
to golang-nuts
Hi rob

Thanks for your response. I didn't know that trick, but it seems to me a little bit ugly to guard fields that way. I will stick to named initialisations and write it as a policy into the style guide.

Cheers

Robert Rodgers

unread,
Jul 11, 2017, 5:26:42 PM7/11/17
to Dan Kortschak, Robert Rodgers, golang-nuts
nice.

Rader Lei

unread,
Jul 11, 2017, 10:44:04 PM7/11/17
to golang-nuts, r...@skyportsystems.com
they both have the same size, eg. following structs bar and bar2 both take 16 bytes memory on a x64 machine.

package main

import (
    "fmt"
    "unsafe"
)

type bar struct {
    A int
    _ bool
}

type bar2 struct {
    A int
    _ [0]byte

}

func main() {
    bar := bar{}
    bar2 := bar2{}
    fmt.Printf("size of bar:%v\n", unsafe.Sizeof(bar))
    fmt.Printf("size of bar2:%v\n", unsafe.Sizeof(bar2))
}

Rader

unread,
Jul 11, 2017, 11:34:40 PM7/11/17
to golang-nuts, r...@skyportsystems.com
I found the position of `[0]byte` in the struct matters. 
 type bar2 struct {
A int
_ [0]byte
}

differs from

type bar3 struct {
_ [0]byte
A int
}



see the full example

Matthew Zimmerman

unread,
Jul 12, 2017, 7:44:47 AM7/12/17
to Rader, golang-nuts, r...@skyportsystems.com
Why not use struct{}?  Is what is recommended for maps to notate a set (only the keys mean something).

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rader

unread,
Jul 13, 2017, 2:20:26 AM7/13/17
to golang-nuts, rade...@gmail.com, r...@skyportsystems.com
sure you can. no memory space difference.

Dmitriy Cherchenko

unread,
Jul 13, 2017, 11:11:14 PM7/13/17
to golang-nuts
Can you do a test to see if there's a performance difference?

Dan Kortschak

unread,
Jul 16, 2017, 9:22:19 PM7/16/17
to Matthew Zimmerman, Rader, golang-nuts, r...@skyportsystems.com
Both are used.

Dan Kortschak

unread,
Jul 16, 2017, 9:28:03 PM7/16/17
to Rader, golang-nuts, r...@skyportsystems.com

roger peppe

unread,
Jul 17, 2017, 8:40:29 AM7/17/17
to snmed, golang-nuts
Sometimes I'll use non-named-member initialisation for
small structs with a well known set of fields (for example,
a Point with X and Y coordinates), but otherwise I much
prefer named initialisation (go vet will warn if you don't, in fact).
I'll almost always put each field onto its own line too - I think
it's more readable that way.

For example:

a2 := A{
name: "Mueller",
firstName: "Max",
}

It's easier to add new members that way, and the
line is shorter.

  cheers,
    rog.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages