I am trying to find if a variable is of type float64:
package main
import ("fmt")
func main() {
myvar := 12.34
if myvar.(type) == float64 {
fmt.Println("Type is float64.")
}
}
However, it is not working and giving following error:
./rnFindType.go:6:10: use of .(type) outside type switch
./rnFindType.go:6:21: type float64 is not an expression
What is the problem and how can it be solved?
You know that myvar
is a float64
because the variable is declared with the concrete type float64
.
If myvar
is an interface type, then you can use a type assertion to determine if the concrete value is some type.
var myvar interface{} = 12.34
if _, ok := myvar.(float64); ok {
fmt.Println("Type is float64.")
}
Try this program at https://play.golang.org/p/n5ftbp5V2Sx