英文:
go: type assert of interface
问题
我有一个关于创建结构体动态模型的问题。我的意思是,根据传入的数据结构,我想要断言、转换或者仅仅改变结构体的类型。
如果sourceName
变量是type_x
,那么deserializedData
的类型应该是type_x
;如果是type_y
,那么就是type_y
。如何动态地设置变量deserializedData
?
我代码中的这部分如下:
....
var cacheData []byte
var deserializedData models.NoaggModel
cache_err := cache.Get(string(string(sourceName) + "_" + string(t.Date)), &cacheData)
if cache_err != nil {
fmt.Println("cache_error: ", cache_err)
panic("the cache is empty")
}
err2 := json.Unmarshal([]byte(cacheData), &deserializedData)
if err2 == nil {
fmt.Println("deserialized data: " + string(sourceName), deserializedData)
}
for _, chart := range charts {
w.Name = chart.Name
if err2 == nil {
w.Data = countDataByName(sourceName, deserializedData, t.Request.Filters, string(chart.Name))
}
out <- w
}
....
如何修改它,以避免严格设置models.NoaggModel
类型?
英文:
I have a problem with making dynamic model of a struct. I mean that I want to assert or cast, or just change the type of struct according to the incoming data strut.
if sourceName
variable would be type_x
, than the type of deserializedData
should be type_x
, if type_y
, than type_y
. How to set the variable deserializedData
dynamicly for this ?
I have this part in my code:
....
var cacheData []byte
var deserializedData models.NoaggModel
cache_err := cache.Get(string(string(sourceName) + "_" + string(t.Date)), &cacheData);
if cache_err != nil {
fmt.Println("cache_error: ", cache_err)
panic("the cache is empty")
}
err2 := json.Unmarshal([]byte(cacheData), &deserializedData)
if err2 == nil {
fmt.Println("deserialized data: " + string(sourceName), deserializedData)
}
for _, chart := range charts {
w.Name = chart.Name
if err2 == nil {
w.Data = countDataByName(sourceName, deserializedData, t.Request.Filters, string(chart.Name))
}
out <- w
}
....
How to modify it, to avoid setting models.Noagg Model
type in a strict way?
答案1
得分: 4
在运行时动态创建类型的实例可以使用 reflect 包来实现。你可以使用一个映射(map)来存储应该能够创建的不同类型:
示例代码:
package main
import (
"fmt"
"reflect"
)
type Foo struct {
Foo string
}
type Bar struct {
Bar int
}
func main() {
var sourceTypes = map[string]reflect.Type{
"foo": reflect.TypeOf(Foo{}),
"bar": reflect.TypeOf(Bar{}),
}
sourceName := "foo"
var deserializedData interface{}
deserializedData = reflect.New(sourceTypes[sourceName]).Interface()
fmt.Printf("%#v", deserializedData)
}
输出结果:
&main.Foo{Foo:""}
Playground: http://play.golang.org/p/qeDA4cu5et
英文:
Creating an instance of a type dynamically during runtime can be done using the reflect package. You can use a map to store the different types that you should be able to create:
Example:
package main
import (
"fmt"
"reflect"
)
type Foo struct {
Foo string
}
type Bar struct {
Bar int
}
func main() {
var sourceTypes = map[string]reflect.Type{
"foo": reflect.TypeOf(Foo{}),
"bar": reflect.TypeOf(Bar{}),
}
sourceName := "foo"
var deserializedData interface{}
deserializedData = reflect.New(sourceTypes[sourceName]).Interface()
fmt.Printf("%#v", deserializedData)
}
Output:
> &main.Foo{Foo:""}
Playground: http://play.golang.org/p/qeDA4cu5et
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论