If I have a custom type that simply redefines a pre-defined type with a name:
type Answer string
And I try to use it in a function that accepts the pre-defined type:
func acceptMe(str string) {
fmt.Println(str)
}
func main() {
type Answer string
var ans Answer = "hello"
// cannot use ans (type Answer) as type string in function argument
acceptMe(ans)
// invalid type assertion: ans.(string) (non-interface type Answer on left)
acceptMe(ans.(string))
// Does work, but I don't understand why if the previous doesn't:
acceptMe(string(ans))
}
Why does the type assertion fail, but the conversion work?