Struct literals

suggest change

A value of a struct type can be written using a struct literal that specifies values for its fields.

type Point struct { X, Y int }
p := Point{1, 2}

The example above lists every field, in order. That is brittle, because the order has to match the type. More often, a struct is initialized by listing some or all of the field names and their values.

anim := gif.GIF{LoopCount: nframes}

Omitted fields are set to the zero value for their type.

Note: the two forms cannot be mixed in the same literal.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents