0
0
Goprogramming~10 mins

Struct usage patterns in Go - Interactive Code Practice

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

Complete the code to declare a struct named Person.

Go
type Person [1] {
    Name string
    Age  int
}
Drag options to blanks, or click blank then click option'
Astruct
Bclass
Cinterface
Dpackage
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of 'struct' which is not valid in Go.
Using 'interface' which defines behavior, not data structure.
2fill in blank
medium

Complete the code to create a new Person instance with Name "Alice" and Age 30.

Go
p := [1] Person{Name: "Alice", Age: 30}
Drag options to blanks, or click blank then click option'
Anew
B*
C&
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new' without assigning fields causes zero values.
Using '*' is for dereferencing, not creating.
3fill in blank
hard

Fix the error in the method receiver syntax to correctly define a method for Person.

Go
func (p [1] Person) Greet() string {
    return "Hello, " + p.Name
}
Drag options to blanks, or click blank then click option'
Afunc
B&
Cvar
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' which is invalid syntax in method receivers.
Using 'var' or 'func' which are keywords, not receiver types.
4fill in blank
hard

Fill both blanks to create a map from string to Person struct and add an entry.

Go
people := make(map[1]Person)
people["bob"] = [2]{Name: "Bob", Age: 25}
Drag options to blanks, or click blank then click option'
A[string]
BPerson
Cstruct
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' inside make's type brackets which is incorrect.
Using 'Person' instead of 'struct' for the literal when type is not declared.
5fill in blank
hard

Fill all three blanks to define an anonymous struct, assign values, and print the Name field.

Go
user := [1] {
    Name: "Eve",
    Age:  28,
}
fmt.Println(user.[2])

var info [3] = user
Drag options to blanks, or click blank then click option'
Astruct
BName
Cstruct { Name string; Age int }
DPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Person' type when anonymous struct is expected.
Trying to access a field not declared in the struct.