Handle _ in switch case

package main

import "fmt"

func do(i interface{}) {
	switch a := i.(type) {
	case int:
		fmt.Printf("int")
	case string:
		fmt.Printf("string")
	default:
		fmt.Printf("I don't know about type")
	}
}

func main() {
	do(21)
	do("hello")
	do(true)
}

The above code will not work as var a is declared but not used.
Switch case will not allow me to use _.
But I don’t want that data I just want that type.

can we do this in another way?

Thanks.

1 Like

Just using switch i.(type) { … } works for me:

https://play.golang.org/p/2d3n2_fbqFk

2 Likes

Thanks.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.