0
0
Goprogramming~10 mins

Creating struct values in Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a struct value with default zero values.

Go
type Person struct {
    Name string
    Age  int
}

func main() {
    p := [1]{}
    fmt.Println(p)
}
Drag options to blanks, or click blank then click option'
Aperson
BPerson
Cstruct
Dnew
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'person' instead of 'Person'.
Using 'new' keyword which returns a pointer, not a value.
2fill in blank
medium

Complete the code to create a struct value with field values.

Go
type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{
        Name: [1],
        Age:  30,
    }
    fmt.Println(p)
}
Drag options to blanks, or click blank then click option'
AName
BAlice
C'Alice'
D"Alice"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted string which causes a compile error.
Using single quotes which are for rune literals.
3fill in blank
hard

Fix the error in creating a struct value with field names.

Go
type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{
        [1]: "Bob",
        Age:  25,
    }
    fmt.Println(p)
}
Drag options to blanks, or click blank then click option'
Aname
BAGE
CName
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase field names which do not match struct fields.
Using all uppercase which is incorrect.
4fill in blank
hard

Fill both blanks to create a struct value with fields and print the Name.

Go
type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{
        [1]: "Carol",
        [2]: 40,
    }
    fmt.Println(p.Name)
}
Drag options to blanks, or click blank then click option'
AName
BAge
Cname
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase field names causing errors.
Mixing up field names.
5fill in blank
hard

Fill all three blanks to create a struct value, update Age, and print the updated struct.

Go
type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{
        [1]: "Dave",
        [2]: 28,
    }
    p.[3] = 29
    fmt.Println(p)
}
Drag options to blanks, or click blank then click option'
AName
BAge
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase field names.
Forgetting to update the Age field correctly.