Вопрос: Как работает type switch и зачем он нужен?
Go
Junior
Без компании
Вопрос: Как работает type switch и зачем он нужен?
Ответы
type switch позволяет ветвиться по динамическому типу interface.
```go
package main
import "fmt"
func printType(x any) {
switch v := x.(type) {
case int:
fmt.Println("int", v)
case string:
fmt.Println("string", v)
default:
fmt.Printf("other %T\n", v)
}
}
func main() {
printType(10)
printType("hi")
printType(true)
}
```