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 above example specifies every field in the right order. Which is not useful, because programmers have to remember the exact fields in order. More often, a struct can be initialized by listing some or all of the field names and their corresponding values.

anim := gif.GIF{LoopCount: nframes}

Omitted fields are set to the zero value for its 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