Step 1 of 5 · Reading · ~4 min
Learn
Collections
Structs
A slice holds many of the same thing; a struct holds the different parts of one thing. By the end of this lesson you can declare a struct type, build values of it from data you read, reach its fields with a dot, and hang a method off it.
Declaring a type, building a value
type NAME struct { ... } defines a new type; the lines inside it are fields, each with a name
and a type. Fields that share a type can share the line: Copies, Shelves int.
There is no constructor to call and no null to guard against. A struct's zero value is just its fields' zero values, and it is usable the moment it exists. Read and write fields with a dot:
Field names follow the same capitalization rule as everything else in Go: Copies is visible
outside the package, copies would not be - and %+v only prints what it can see.
A struct is a value, not a reference
Assigning a struct copies every field. So does passing one to a function:
That is the trap: the code looks like it worked and the value never moved. Until pointers (next lesson), the fix is to hand the new value back:
Methods: a function with a receiver
A receiver is a parameter written before the function name; it makes the function a method of that type and gives it dot-call syntax:
(r Rect) reads "this belongs to Rect". Inside the body, r is the value it was called on.
From loose input to a struct
A whole program that reads two numbers, groups them, and asks the group for an answer:
Feed it 3 then 5 and it prints 15. Note the shape: plain values come in, one struct is built
from them, and from there the arithmetic is spelled in field names instead of loose variables.
Structs nest, too - a field may be another struct (type Shelf struct { Room string; Top Book },
read as s.Top.Copies). Go has no inheritance at all: you build big types by composing small ones.
Common mistakes
Take two points, A at (2, 3) and B at (5, 7). The squared distance between them is 25.
- Never squaring. Adding the steps across and up prints
7- that is the walking distance along the grid, not this quantity. - Squaring the sum instead of summing the squares.
(3 + 4) * (3 + 4)prints49. Each difference is squared on its own, then the two squares are added. - Using the coordinates instead of the differences. Squaring B's own numbers prints
74. This one agrees with the right answer whenever the first point happens to sit at the origin, so it survives an easy test and fails everywhere else. - Crossing the fields - subtracting A's Y from B's X. That prints
29. Keep X with X and Y with Y; a struct lets you check that by reading the field names.
Your exercise
The starter defines Point with X, Y int, reads four coordinates into plain ints, and prints a
placeholder. Replace the placeholder so the program reports the squared distance between the two
points as a single integer - math is not needed and an unused import is a compile error. The
mistake to watch for is doing the arithmetic on the coordinates rather than on the differences.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…