0
0
Goprogramming~10 mins

Empty interface 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 variable that can hold any type using the empty interface.

Go
var data [1]
Drag options to blanks, or click blank then click option'
Aint
Binterface{}
Cstring
Dstruct{}
Attempts:
3 left
💡 Hint
Common Mistakes
Using a concrete type like int or string instead of the empty interface.
Using struct{} which is an empty struct, not an interface.
2fill in blank
medium

Complete the code to assign a string value to an empty interface variable.

Go
var value interface{}
value = [1]
Drag options to blanks, or click blank then click option'
A"hello"
B100
Ctrue
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a number or boolean when a string is expected.
Forgetting to use quotes around the string.
3fill in blank
hard

Fix the error in the code by completing the type assertion from an empty interface to int.

Go
var data interface{} = 42
num := data.[1]
Drag options to blanks, or click blank then click option'
A(int)
Bcast
Ctype
Dassert
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cast' or 'type' which are not Go keywords for this.
Trying to use slice notation like ([]int).
4fill in blank
hard

Fill both blanks to create a map from string to empty interface and assign a value.

Go
data := map[string][1]{
	"key": [2],
}
Drag options to blanks, or click blank then click option'
Ainterface{}
B"value"
Cint
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Using int as the map value type when it should be interface{}.
Assigning a string value without quotes.
5fill in blank
hard

Fill all three blanks to create a slice of empty interfaces and append a boolean value.

Go
var items [][1]
items = append(items, [2])
fmt.Println(items[[3]])
Drag options to blanks, or click blank then click option'
Ainterface{}
Btrue
C0
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using false instead of true for the appended value.
Using 1 instead of 0 for the slice index.