What is the syntax for a type switch statement in Go?
- switch x.(type) { case Type1: // code break case Type2: // code break default: // code }
- switch x.(type) { case Type1: // code case Type2: // code default: // code }
- switch x.(type) { case Type1: // code continue case Type2: // code continue default: // code }
- switch x.(type) { case Type1: // code fallthrough case Type2: // code fallthrough default: // code }
The correct syntax for a type switch statement in Go is switch x.(type) { case Type1: // code case Type2: // code default: // code }. Type switches in Go allow you to perform different actions based on the type of an interface variable. It evaluates the type of x and compares it with each case. If a match is found, the corresponding code block is executed. If none of the cases match, the default case (if provided) is executed.
Loading...
Related Quiz
- In Go, the _________ function is executed automatically before the main function in the same package.
- How can channels be used to synchronize Goroutines?
- The _______ data type in Go is used to represent a sequence of bytes.
- _______ in Go allows a struct to anonymously embed other structs.
- Which of the following is a valid way to initialize a variable in Go?