Quick actions

cmd+k|ctrl+k

Navigation

Languages

if, switch, goto / switch statement / Essential Go

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-03-26T10:14:09Z

Updated

2019-03-26T10:14:09Z

package main

import (
	"fmt"
)

func printType(iv interface{}) {
	// inside case statements, v is of type matching case type
	switch v := iv.(type) {
	case int:
		fmt.Printf("'%d' is of type int\n", v)
	case string:
		fmt.Printf("'%s' is of type string\n", v)
	case float64:
		fmt.Printf("'%f' is of type float64\n", v)
	default:
		fmt.Printf("We don't support type '%T'\n", v)
	}
}

func main() {
	printType("5")
	printType(4)
	printType(true)
}
INFO