Golang Structs in 5 Minutes
Go structs for beginners, in five minutes, on a real Go program: why adding one field breaks every struct literal without field names, zero values, keyed literals, why assigning a struct copies it, exported fields and JSON tags, and the struct tag typo that only go vet catches.
One new field, three errors
You added one field to a Go struct, and now every line that builds a value of it stops compiling. Sue keeps her arcade's high score board in Go. Rich adds a Date field to the Score struct. One line.

Sue runs the program with go run, and Go stops: too few values in struct literal of type Score, three times. Rich added one line. Go found three things to say about it. By the end, you'll know why, how to write structs that don't break, and the one extra space that silently renames a field.
go run .# arcade ./main.go:14:30: too few values in struct literal of type Score ./main.go:15:31: too few values in struct literal of type Score ./main.go:16:32: too few values in struct literal of type Score
What a struct is
To see why one line broke three, start with what a struct is. Picture the prize counter at Sue's arcade. There's a stack of blank entry cards, each with the same labelled boxes: name, points, game. Every card a player fills in is its own copy.

In Go, the blank card is a struct type. Sue writes type Score struct, then one line per field: a name, then a type. Player is a string, which is text. Points is an int, a whole number. Game and Rich's new Date are strings too. Each score is a value of that type, with all four fields.

So what's in the boxes before anyone writes in them? Sue declares var s Score and prints it twice. Println shows only the values: blanks and a zero. Printf, with percent, plus, v in its format, also shows each field's name, Date included. In Go, every field starts at its type's zero value: empty for a string, zero for a number.
sed -n '13,15p' main.govar s Score fmt.Println(s) fmt.Printf("%+v\n", s)
go run .{ 0 } {Player: Points:0 Game: Date:}
Write the field names
Here's why one new field broke three lines. Code that builds a struct value in braces is called a struct literal. The board has three, for Sue, Rich and May, each written as bare values: SUE, 98200, Galaxy Run. With no field names, Go matches values to fields by position: first to Player, second to Points, third to Game. So it needs one value for every field. Score has four now, and every line is one short.

The Go spec calls field names in a literal keys. Without keys, a struct literal needs a value for every field, in order. With keys, you can leave fields out, and they get their zero value.

So Sue writes each field's name before its value. Rich's score gets its date. Sue's and May's simply leave it out.

Now it builds. Sue's and May's scores show an empty Date, its zero value, and Rich's shows November twenty-second.
go run .{Player:SUE Points:98200 Game:Galaxy Run Date:} {Player:RICH Points:87150 Game:Galaxy Run Date:Nov 22} {Player:MAY Points:64300 Game:Pinball Pete Date:}
A copy, not the original
Sue beats her own record. The board is a slice: a list of scores. She copies the first one, at index zero, into a new variable called top, and sets top.Points to 99,950. Guess first: what does the board's own score print now? It's 98,200. The old score. Assigning a struct copies every field into a new value, so Sue changed the copy, not the board.
sed -n '17,20p' main.gotop := board[0] top.Points = 99950 fmt.Println(top.Points) fmt.Println(board[0].Points)
go run .99950 98200
To change the score on the board itself, she adds an ampersand, which gives the location of that score instead of a copy. top is now a pointer: an arrow to the one score on the board. Now top.Points and the board's own score both print 99,950.
sed -n '17,20p' main.gotop := &board[0] top.Points = 99950 fmt.Println(top.Points) fmt.Println(board[0].Points)
go run .99950 99950
Capital letters and tags
The arcade's lobby screen needs Rich's date too, and whether it gets there comes down to one letter. The screen reads scores as JSON, a plain text format programs share. Sue adds a short note in backticks after each field, called a tag. It tells Go's JSON package what to call that field. Now suppose Rich had typed his field as lowercase date, tag and all. The output has player, points and game, and no date. The tag isn't the problem. The small d is. In Go, a package is one folder of code. A field that starts lowercase is unexported: only code in its own package can see it, and the JSON package is a different one.
sed -n '8,13p' main.gotype Score struct { Player string `json:"player"` Points int `json:"points"` Game string `json:"game"` date string `json:"date"` }
go run .{"player":"RICH","points":87150,"game":"Galaxy Run"}
A field that starts with a capital letter is exported: any package can see it. With a capital D, the date reaches the lobby screen.
sed -n '12,12p' main.goDate string `json:"date"`
go run .{"player":"RICH","points":87150,"game":"Galaxy Run","date":"Nov 22"}
And that extra space. Sue puts a space between the colon and the quote marks. Go still builds and runs, but the key comes out as Player, with a capital P. The tag was ignored, so JSON fell back to the field's own name. go vet, Go's built-in checker for suspicious code, catches it: bad syntax for struct tag value.
sed -n '9,9p' main.goPlayer string `json: "player"`
go run .{"Player":"RICH","points":87150,"game":"Galaxy Run","date":"Nov 22"}
go vet .main.go:9:2: struct field tag `json: "player"` not compatible with reflect.StructTag.Get: bad syntax for struct tag value
Recap
So: name the fields in your literals, remember that assigning copies and an ampersand reaches the original, capitalize what other packages need, and run go vet.

If structs make sense now, subscribing brings the next Go idiom: for range loops.
All the lab files and commands are linked in the description. Thanks for watching Cloud Explainers.
Sources
- The Go Programming Language Specification, Struct types
- The Go Programming Language Specification, Composite literals (struct literals)
- The Go Programming Language Specification, The zero value
- The Go Programming Language Specification, Exported identifiers
- Package encoding/json, Marshal
- go vet structtag check
Demos: one container on our own Mac standing in for a laptop; the arcade and its scores are made up. Every command, error and number was real. Watch the full video on YouTube.