I'm using to fmt.Sscan
convert a string to any type, here is what I'm doing:
package main
import (
"fmt"
"reflect"
)
func test() interface{} {
return 0
}
func main() {
a := test() // this could be any type
v := "10" // this could be anything
fmt.Println(reflect.TypeOf(a), reflect.TypeOf(&a))
_, err := fmt.Sscan(v, &a)
fmt.Println(err)
}
This code is failing because Sscan
doesn't accept interfaces as the second value: can't scan type: *interface {}
. demo
What I find most weird is that the first print prints: int *interface {}
, is it a int or an interface?
How can I assert a
to the right type (it could be any primitive)? Is there a solution that doesn't include a giant switch statement?
Thank you.