Source file tour/methods/type-switches.go

     1  //go:build OMIT
     2  
     3  package main
     4  
     5  import "fmt"
     6  
     7  func do(i interface{}) {
     8  	switch v := i.(type) {
     9  	case int:
    10  		fmt.Printf("Twice %v is %v\n", v, v*2)
    11  	case string:
    12  		fmt.Printf("%q is %v bytes long\n", v, len(v))
    13  	default:
    14  		fmt.Printf("I don't know about type %T!\n", v)
    15  	}
    16  }
    17  
    18  func main() {
    19  	do(21)
    20  	do("hello")
    21  	do(true)
    22  }
    23  

View as plain text