How to use type switch to in Go

1 Answer

0 votes
package main

import "fmt"

func type_switches(i interface{}) {
	switch val := i.(type) {
		case int:
			fmt.Printf("int: %v\n", val)
		case string:
			fmt.Printf("string: %q\n", val)
		default:
			fmt.Printf("default: %T\n", val)
	}
}

func main() {
	type_switches(83445)
	type_switches("go")
	type_switches(true)
}



/*
run:

int: 83445
string: "go"
default: bool

*/

 



answered Mar 31, 2020 by avibootz
...