Go code
type Dog struct { Name string }
func (d Dog) Speak() {
fmt.Println(d.Name, "says woof")
}
d := Dog{Name: "Milo"}
d.Speak()Line-by-line explanation
- Dog has a Name field.
- The receiver d attaches Speak to Dog.
- The method reads d.Name.
- Dot notation calls it on one value.
Expected output
Milo says woofThis is the expected result.
Tiny Practice
Starter scenario
Call the existing Speak method.
Go code
d := Dog{Name: "Milo"}
d.CHANGE_MELine-by-line explanation
- The Dog value is ready.
- The method call is missing.
Expected output
(complete the starter first)The starter intentionally contains CHANGE_ME.
Hint
- Use
Speak. - Add round brackets.
- Capitalization matters.
Show Solution
Go code
d := Dog{Name: "Milo"}
d.Speak()Line-by-line explanation
- D stores a Dog.
- Speak is called on it.
Expected output
Milo says woofThis is the expected result.