0
0
Goprogramming~10 mins

Why structs are used in Go - Test Your Understanding

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' because Go does not have classes.
Using 'interface' which is for behavior, not data grouping.
2fill in blank
medium

Complete the code to create a variable of type Person.

Go
var p [1] Person
Drag options to blanks, or click blank then click option'
A=
Bnew
Cvar
D:=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' without declaration.
Using 'var' keyword incorrectly here.
3fill in blank
hard

Fix the error in the code to access the Age field of a Person struct.

Go
fmt.Println(p[1]Age)
Drag options to blanks, or click blank then click option'
A.
B->
C::
D#
Attempts:
3 left
💡 Hint
Common Mistakes
Using '->' which is from other languages like C.
Using '::' or '#' which are invalid in Go for field access.
4fill in blank
hard

Complete the code to create a struct literal for Person with Name "Alice" and Age 30.

Go
p := Person{Name: "Alice", [1]: 30}
Drag options to blanks, or click blank then click option'
A{
BAge
C}
D[
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of braces.
Omitting the field name for the second value.
5fill in blank
hard

Fill all three blanks to define a struct, create a variable, and assign a value.

Go
type [1] struct {
    Name string
}

var p [2] [3]
Drag options to blanks, or click blank then click option'
APerson
Bp
DPerson{}
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name as the type.
Not assigning a struct literal when declaring the variable.