📖 Scenario: You are creating a simple program to store information about a book and its author. The author has a name and an age. The book has a title and an author.
🎯 Goal: You will build a Go program that uses nested structs to represent a book and its author, then print the book's title and the author's name.
📋 What You'll Learn
Create a struct called Author with fields Name string and Age int.
Create a struct called Book with fields Title string and Author Author.
Create a variable myBook of type Book with title "Go Programming" and author name "Alice" aged 30.
Print the book's title and the author's name exactly as shown.
💡 Why This Matters
🌍 Real World
Nested structs are useful to model real-world objects that have parts or details inside them, like a book having an author.
💼 Career
Understanding nested structs is important for Go developers working on data modeling, APIs, and complex data structures.
Progress0 / 4 steps
1
Define the Author struct
Write a struct called Author with two fields: Name of type string and Age of type int.
Go
Hint
Use type Author struct {} and add the fields inside the braces.
2
Define the Book struct with nested Author
Write a struct called Book with two fields: Title of type string and Author of type Author (the struct you defined earlier).
Go
Hint
Use type Book struct { Title string; Author Author } to nest the Author struct inside Book.
3
Create a variable myBook with nested data
Inside main(), create a variable called myBook of type Book. Set its Title to "Go Programming" and its Author to an Author struct with Name"Alice" and Age30.
Go
Hint
Use composite literals to set nested struct fields, like myBook := Book{ Title: "Go Programming", Author: Author{Name: "Alice", Age: 30} }.
4
Print the book title and author name
Add a fmt.Println statement inside main() to print the book's title and the author's name in this exact format: "Title: Go Programming, Author: Alice". Remember to import the fmt package.
Go
Hint
Use fmt.Println("Title: ", myBook.Title, ", Author: ", myBook.Author.Name) to print the values.
Practice
(1/5)
1. What is the main purpose of using nested structs in Go?
easy
A. To group related data together inside another struct
B. To create multiple independent structs
C. To make the program run faster
D. To avoid using functions
Solution
Step 1: Understand struct grouping
Nested structs allow one struct to contain another, grouping related data logically.
Step 2: Compare options
Creating multiple independent structs, making the program run faster, or avoiding functions do not describe grouping data inside structs.
Final Answer:
To group related data together inside another struct -> Option A
Quick Check:
Nested structs = Group related data [OK]
Hint: Think of nested structs as folders inside folders [OK]
Common Mistakes:
Confusing nested structs with separate structs
Thinking nested structs improve speed
Believing nested structs replace functions
2. Which of the following is the correct way to access the field City inside a nested struct Address within struct Person?
easy
A. person.City.Address
B. person->Address->City
C. person.Address.City
D. person.Address->City
Solution
Step 1: Recall Go syntax for nested structs
Access nested fields using dot notation: Outer.Inner.Field.
Step 2: Evaluate options
person.Address.City uses correct dot notation. Arrow syntax (->) is invalid in Go. person.City.Address reverses the order.
Final Answer:
person.Address.City -> Option C
Quick Check:
Dot notation = person.Address.City [OK]
Hint: Use dot notation to access nested fields [OK]
Common Mistakes:
Using arrow (->) instead of dot (.)
Reversing the order of fields
Trying to access nested fields directly without parent
3. What will be the output of the following Go code?
package main
import "fmt"
type Address struct {
City string
Zip int
}
type Person struct {
Name string
Address Address
}
func main() {
p := Person{Name: "Anna", Address: Address{City: "Paris", Zip: 75000}}
fmt.Println(p.Address.City)
}
medium
A. Anna
B. Paris
C. 75000
D. Address
Solution
Step 1: Understand struct initialization
Person struct has Name and Address fields. Address is initialized with City "Paris" and Zip 75000.
Step 2: Identify printed field
The code prints p.Address.City, which is "Paris".
Final Answer:
Paris -> Option B
Quick Check:
p.Address.City = Paris [OK]
Hint: Print nested field with dot notation [OK]
Common Mistakes:
Printing the wrong field like Name or Zip
Confusing struct field names
Expecting the whole struct to print
4. Identify the error in this Go code snippet involving nested structs:
package main
import "fmt"
type Engine struct {
Power int
}
type Car struct {
Model string
Engine Engine
}
func main() {
c := Car{Model: "Sedan"}
fmt.Println(c.Engine.Power)
}
medium
A. c.Engine.Power is zero value, no error
B. Car struct missing Engine field
C. Engine struct is not defined
D. Cannot print nested struct fields directly
Solution
Step 1: Check struct definitions
Engine and Car structs are defined correctly. Car has Engine field.
Step 2: Analyze initialization and print
Car instance c is created with Model only. Engine field is zero-valued (Power = 0). Printing c.Engine.Power prints 0 without error.
Final Answer:
c.Engine.Power is zero value, no error -> Option A
Quick Check:
Uninitialized nested struct fields have zero values [OK]
Hint: Uninitialized nested structs have zero values [OK]
Common Mistakes:
Assuming missing initialization causes error
Thinking nested fields can't be printed
Confusing missing fields with zero values
5. Given the structs below, how would you write a function FullAddress that returns a formatted string combining Street, City, and Zip from nested structs?
type Location struct {
Street string
City string
}
type Address struct {
Location Location
Zip int
}