Bird
Raised Fist0
Goprogramming~20 mins

Creating struct values in Go - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Struct Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of struct literal with field names
What is the output of this Go program?
Go
package main
import "fmt"
type Person struct {
    Name string
    Age  int
}
func main() {
    p := Person{Name: "Alice", Age: 30}
    fmt.Println(p)
}
A{Name: Alice, Age: 30}
B{Alice 30}
CPerson{Name: "Alice", Age: 30}
D{Name:Alice Age:30}
Attempts:
2 left
💡 Hint
Look at how fmt.Println prints structs by default.
❓ Predict Output
intermediate
2:00remaining
Output of struct literal without field names
What is the output of this Go program?
Go
package main
import "fmt"
type Point struct {
    X int
    Y int
}
func main() {
    p := Point{10, 20}
    fmt.Println(p)
}
APoint{10, 20}
B{X=10, Y=20}
C{10 20}
D{X:10 Y:20}
Attempts:
2 left
💡 Hint
Struct literals can omit field names if values are in order.
❓ Predict Output
advanced
2:00remaining
Output of nested struct values
What is the output of this Go program?
Go
package main
import "fmt"
type Address struct {
    City  string
    State string
}
type User struct {
    Name    string
    Address Address
}
func main() {
    u := User{Name: "Bob", Address: Address{City: "NY", State: "NY"}}
    fmt.Println(u)
}
AUser{Name: "Bob", Address: Address{City: "NY", State: "NY"}}
B{Name:Bob Address:{City:NY State:NY}}
C{Bob {City:NY State:NY}}
D{Bob {NY NY}}
Attempts:
2 left
💡 Hint
fmt.Println prints nested structs recursively without field names.
❓ Predict Output
advanced
2:00remaining
Output of pointer to struct literal
What is the output of this Go program?
Go
package main
import "fmt"
type Car struct {
    Brand string
    Year  int
}
func main() {
    c := &Car{Brand: "Tesla", Year: 2022}
    fmt.Println(*c)
}
A{Tesla 2022}
B&{Tesla 2022}
C{Brand:Tesla Year:2022}
DCar{Brand: "Tesla", Year: 2022}
Attempts:
2 left
💡 Hint
Dereferencing pointer prints the struct value.
🧠 Conceptual
expert
3:00remaining
Number of fields in struct literal with embedded struct
Given the structs below, how many fields does the struct literal have when creating a value of type Employee?
Go
type Person struct {
    Name string
    Age  int
}
type Employee struct {
    Person
    ID int
}

// Creating value:
// e := Employee{Person{Name: "Eve", Age: 28}, 101}
A2 fields: Person and ID
B3 fields: Name, Age, and ID
C1 field: ID only
D4 fields: Person, Name, Age, and ID
Attempts:
2 left
💡 Hint
Embedded structs count as one field in the outer struct literal.

Practice

(1/5)
1. What is the correct way to create a struct value with fields Name as "Alice" and Age as 30 in Go?
easy
A. person := Person("Alice", 30)
B. person := Person{30, "Alice"}
C. person := Person{Name: "Alice", Age: 30}
D. person := Person{Name = "Alice", Age = 30}

Solution

  1. Step 1: Understand struct literal syntax

    In Go, struct values are created using the syntax StructType{FieldName: value, ...} for clarity and correctness.
  2. Step 2: Match the syntax with the given options

    person := Person{Name: "Alice", Age: 30} uses the correct syntax with field names and values. Options A and C use positional values without field names, which is allowed but less clear and can cause errors if order changes. person := Person{Name = "Alice", Age = 30} uses '=' which is invalid in struct literals.
  3. Final Answer:

    person := Person{Name: "Alice", Age: 30} -> Option C
  4. Quick Check:

    Use field names with colons in struct literals = B [OK]
Hint: Use field names with colons to create structs clearly [OK]
Common Mistakes:
  • Using parentheses instead of braces
  • Using '=' instead of ':' in struct literals
  • Mixing field order without names causing confusion
2. Which of the following is the correct syntax to create a struct value with zero values for all fields in Go?
easy
A. p := Person{}
B. p := Person()
C. p := new(Person)
D. p := Person(nil)

Solution

  1. Step 1: Recall zero value struct creation

    In Go, creating a struct with zero values for all fields is done by using empty braces after the struct type: StructType{}.
  2. Step 2: Evaluate options

    p := Person{} uses Person{} which creates a struct with zero values. p := Person() uses parentheses which is invalid syntax. p := new(Person) uses new(Person) which returns a pointer, not a struct value. p := Person(nil) uses nil which is invalid for struct values.
  3. Final Answer:

    p := Person{} -> Option A
  4. Quick Check:

    Empty braces create zero-value struct = C [OK]
Hint: Use empty braces {} to create zero-value struct [OK]
Common Mistakes:
  • Using parentheses instead of braces
  • Confusing new() pointer with struct value
  • Passing nil to struct literal
3. Given the struct and code below, what will be printed?
type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{Name: "Bob", Age: 25}
    p.Age = 26
    fmt.Println(p)
}
medium
A. {Bob 25}
B. {Bob 26}
C. Person{Name: "Bob", Age: 26}
D. Syntax error

Solution

  1. Step 1: Create struct and modify field

    The struct Person is created with Name "Bob" and Age 25. Then the Age field is updated to 26.
  2. Step 2: Print the struct value

    Printing the struct with fmt.Println shows the field values in braces. Since Age was updated to 26, the output is {Bob 26}.
  3. Final Answer:

    {Bob 26} -> Option B
  4. Quick Check:

    Updated field value shows in output = D [OK]
Hint: Changing a field after creation updates printed struct [OK]
Common Mistakes:
  • Assuming original Age remains unchanged
  • Expecting struct type name in print output
  • Confusing syntax error with valid code
4. What is wrong with the following code snippet?
type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{Name: "Eve", Age: "30"}
    fmt.Println(p)
}
medium
A. Age field has wrong type value
B. Missing parentheses in struct creation
C. Name field is not initialized
D. fmt.Println cannot print structs

Solution

  1. Step 1: Check field types in struct literal

    The struct Person has Age as int type, but the code assigns "30" which is a string, causing a type mismatch error.
  2. Step 2: Identify the error cause

    Assigning a string to an int field is invalid in Go and causes a compile-time error.
  3. Final Answer:

    Age field has wrong type value -> Option A
  4. Quick Check:

    Field types must match exactly = A [OK]
Hint: Match field types exactly when creating struct values [OK]
Common Mistakes:
  • Assigning string to int field
  • Confusing struct literal syntax errors
  • Thinking fmt.Println can't print structs
5. How can you create a slice of struct values representing three people with names "Anna", "Ben", "Cara" and ages 22, 28, 31 respectively in Go?
hard
A. people := []Person{Person("Anna", 22), Person("Ben", 28), Person("Cara", 31)}
B. people := [Person]{Name: "Anna", Age: 22, Name: "Ben", Age: 28, Name: "Cara", Age: 31}
C. people := []Person{Name: "Anna", Age: 22, Name: "Ben", Age: 28, Name: "Cara", Age: 31}
D. people := []Person{{Name: "Anna", Age: 22}, {Name: "Ben", Age: 28}, {Name: "Cara", Age: 31}}

Solution

  1. Step 1: Understand slice of structs syntax

    A slice of structs is created using []StructType{...} with each element as a struct literal.
  2. Step 2: Check each option for correct syntax

    people := []Person{{Name: "Anna", Age: 22}, {Name: "Ben", Age: 28}, {Name: "Cara", Age: 31}} correctly creates a slice with three struct literals using field names. people := [Person]{Name: "Anna", Age: 22, Name: "Ben", Age: 28, Name: "Cara", Age: 31} uses array syntax but incorrect literal format. people := []Person{Name: "Anna", Age: 22, Name: "Ben", Age: 28, Name: "Cara", Age: 31} tries to create slice but misses braces for each struct. people := []Person{Person("Anna", 22), Person("Ben", 28), Person("Cara", 31)} uses parentheses which is invalid for struct literals.
  3. Final Answer:

    people := []Person{{Name: "Anna", Age: 22}, {Name: "Ben", Age: 28}, {Name: "Cara", Age: 31}} -> Option D
  4. Quick Check:

    Slice of structs uses []Type{{...}, {...}} = A [OK]
Hint: Use double braces for slice of struct literals [OK]
Common Mistakes:
  • Using parentheses instead of braces for structs
  • Missing braces around each struct in slice
  • Confusing array and slice syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard