Is there a straightforward way to convert a struct where some fields are "generic" (interface{}
) into another type which has the same field names but is "strongly-typed" (int
, string
, etc)?
Lets say that, given the definitions:
package main
import (
"fmt"
)
type GenericData struct {
Hard int
Soft interface{}
}
type Data struct {
Hard int
Soft int
}
type GenericDataGenerator func() GenericData
func generateGenericData() interface{} {
return GenericData{1, 2}
}
func returnsGenericDataGenerator() interface{} {
return generateGenericData
}
Does converting from GenericData
to Data
require explicitly casting all interface{}
fields? Is there a more straightforwad way to do this?
func main() {
gd := generateGenericData()
fmt.Println(gd)
fmt.Println(gd.(GenericData))
// Doesn't work, but is straightforward
// fmt.Println(gd.(Data))
// Works, but is not straight forward
fmt.Println(Data{gd.(GenericData).Hard, gd.(GenericData).Soft.(int)})
genDataGenerator := returnsGenericDataGenerator()
// Doesn't work, but is straightforward
//genDataGenerator.(GenericDataGenerator)()
// Works, but is not straight forward
resp := genDataGenerator.(func() interface{})()
fmt.Println(resp.(GenericData))
}
The code can be executed in: https://play.golang.org/p/_UVBi5It1FY
Is there a straightforward way to convert a struct where some fields are "generic" (interface{}) into another type which has the same field names but is "strongly-typed" (int, string, etc)?
No.
And this is obvious once you understand that interface{}
is not some kind of "generic" or "arbitrary" type but a single, "strongly-type" type just like string
or int32
. The empty interface interface{}
is a totally normal type just like a uint16
or a map[float32]bool
is. It is a interface type (in contrast to the other examples which are a numeric type and a map type) and its method set is the empty set. It is as strict or strongly typed as any of the other types.
The fact that you can assign any value to a variable of type interface{}
does not mean that interface{}
somehow means "any type". You can assign the values 3
, -1234
and 435948
(to name a few) to a int32
. This is allowed because the allowed value range of a int32
contains these values (3, -1234, 435948). You can assign a value of type func(int) string
to a variable of type interface{}
because the allowed value range of interface{}
contains func(int)string
. This is a vacuous truth and not a "generic" one.