0
0
Goprogramming~10 mins

Default case in select 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 add a default case in the select statement.

Go
select {
case msg := <-ch:
    fmt.Println("Received:", msg)
[1]:
    fmt.Println("No message received")
}
Drag options to blanks, or click blank then click option'
Adefault
Bcase
Celse
Dfallback
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'case' instead of 'default' for the default case.
Using 'else' or 'fallback' which are not valid in select.
2fill in blank
medium

Complete the code to print a message when no channel is ready using the default case.

Go
select {
case <-ch1:
    fmt.Println("ch1 ready")
case <-ch2:
    fmt.Println("ch2 ready")
[1]:
    fmt.Println("No channels ready")
}
Drag options to blanks, or click blank then click option'
Acase else
Bcase default
Celse
Ddefault
Attempts:
3 left
💡 Hint
Common Mistakes
Writing 'case default:' which is invalid syntax.
Using 'else' which is not a Go keyword in select.
3fill in blank
hard

Fix the error in the select statement by completing the default case correctly.

Go
select {
case msg := <-ch:
    fmt.Println("Got message:", msg)
[1]:
    fmt.Println("No message received")
}
Drag options to blanks, or click blank then click option'
Acase default
Bdefault
Ccase else
Delse
Attempts:
3 left
💡 Hint
Common Mistakes
Writing 'case default:' which causes a syntax error.
Using 'else' which is not valid in select.
4fill in blank
hard

Fill both blanks to create a select with two channel cases and a default case.

Go
select {
case msg1 := <-ch1:
    fmt.Println("ch1:", msg1)
case msg2 := <-ch2:
    fmt.Println("ch2:", msg2)
[1]:
    fmt.Println("[2]")
}
Drag options to blanks, or click blank then click option'
Adefault
Bcase
CNo channels ready
DNo message
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'case' before 'default' keyword.
Printing a wrong message.
5fill in blank
hard

Fill all three blanks to write a select that reads from two channels and uses a default case with a message.

Go
select {
case val1 := <-chan1:
    fmt.Println("chan1 value:", [1])
case val2 := <-chan2:
    fmt.Println("chan2 value:", [2])
[3]:
    fmt.Println("No data received")
}
Drag options to blanks, or click blank then click option'
Aval1
Bval2
Cdefault
Dval3
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in print statements.
Writing 'case default' instead of 'default'.