Quick actions

cmd+k|ctrl+k

Navigation

Languages

Essential Go / Empty interface

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-03-26T10:01:21Z

Updated

2019-03-26T10:01:21Z

package main

import (
	"fmt"
)

func printVariableType(v interface{}) {
	switch v.(type) {
	case string:
		fmt.Printf("v is of type 'string'\n")
	case int:
		fmt.Printf("v is of type 'int'\n")
	default:
		// generic fallback
		fmt.Printf("v is of type '%T'\n", v)
	}
}

func main() {
	printVariableType("string") // string
	printVariableType(5)        // int
	printVariableType(int32(5)) // int32
}
INFO