I am new to golang, so appologize if this question is too naive. Looked around, but could not find answer to my basic question.
Lets say I have a concrete struct and methods as shown below.
type MyData struct{
field1 string
field2 int
}
func(a MyData) OperatorOnString() string{
return a.field1.(string)
}
func(a MyData) OperatorOnInt() int{
return a.field2.(int)
}
My question is, can I type cast and return rather than performing assertion? From what I have learned so far is that, assertion is used on data of type interface. But in this case I have concrete type. Should I still use assertion or I can do something like return int(a.field2)
. I know this example is trivial, but the point that I am confused is when to use between the two conversion types. Or is there some golang idiomaticity involved here?
Thanks