[英]Golang dynamically creating member of struct
I know there is struct in Go, but for all I know, you have to define struct我知道 Go 中有 struct,但就我所知,你必须定义 struct
type Circle struct{
x,y,r float64
}
I am wondering how you can declare a new variable that doesn't exist in the struct我想知道如何声明结构中不存在的新变量
circle := new(Circle)
circle.color = "black"
You will need to use a map (of type map[string]interface{}
) to work with dynamic JSON.您将需要使用地图(类型为
map[string]interface{}
)来处理动态 JSON。 Here is an example of creating a new map:以下是创建新地图的示例:
// Initial declaration
m := map[string]interface{}{
"key": "value",
}
// Dynamically add a sub-map
m["sub"] = map[string]interface{}{
"deepKey": "deepValue",
}
Unmarshalling JSON into a map looks like:将 JSON 解组为地图如下所示:
var f interface{}
err := json.Unmarshal(b, &f)
The code above would leave you with a map in f
, with a structure resembling:上面的代码会在
f
留下一张地图,其结构类似于:
f = map[string]interface{}{
"Name": "Wednesday",
"Age": 6,
"Parents": []interface{}{
"Gomez",
"Morticia",
},
}
You will need to use a type assertion to access it, otherwise Go won't know it's a map:您将需要使用类型断言来访问它,否则 Go 将不知道它是一个地图:
m := f.(map[string]interface{})
You will also need to use assertions or type switches on each item you pull out of the map.您还需要在从地图中拉出的每个项目上使用断言或类型开关。 Dealing with unstructured JSON is a hassle.
处理非结构化 JSON 很麻烦。
More information:更多信息:
I've started to work on this small repository https://github.com/Ompluscator/dynamic-struct我已经开始研究这个小型存储库https://github.com/Ompluscator/dynamic-struct
It's possible at this point to extend existing struct in runtime, by passing a instance of struct and modifying fields (adding, removing, changing types and tags).此时可以通过传递结构体的实例和修改字段(添加、删除、更改类型和标签)来在运行时扩展现有结构体。
Still in progress, so don't expect something huge :)仍在进行中,所以不要指望有什么大事:)
EDIT: At this point, work on library is done, and it looks stable for last a couple of months :)编辑:在这一点上,图书馆的工作已经完成,而且过去几个月看起来很稳定:)
You can't.你不能。 Go is statically typed, and does not allow such constructs.
Go 是静态类型的,不允许这样的构造。
Structs have a layout in memory that directly related to the definition, and there's no where to store such additional fields.结构在内存中具有与定义直接相关的布局,并且没有地方存储此类附加字段。
You can use a map instead.您可以改用地图。 Moreover, you can use
&circle
as a key or part of a key, to associate map elements with arbitrary structs.此外,您可以使用
&circle
作为键或键的一部分,将地图元素与任意结构相关联。
type key struct {
target interface{}
field string
}
x := make(map[key]string)
x[key{ target: circle, field: "color" }] = "black"
You can do it using reflect package, check StructOf
method it allows you to create a new struct from []reflect.StructField
.您可以使用反射包来完成,检查
StructOf
方法它允许您从[]reflect.StructField
创建一个新结构。 Example:示例:
func main() {
typ := reflect.StructOf([]reflect.StructField{
{
Name: "Height",
Type: reflect.TypeOf(float64(0)),
Tag: `json:"height"`,
},
{
Name: "Age",
Type: reflect.TypeOf(int(0)),
Tag: `json:"age"`,
},
})
v := reflect.New(typ).Elem()
v.Field(0).SetFloat(0.4)
v.Field(1).SetInt(2)
s := v.Addr().Interface()
w := new(bytes.Buffer)
if err := json.NewEncoder(w).Encode(s); err != nil {
panic(err)
}
fmt.Printf("value: %+v\n", s)
fmt.Printf("json: %s", w.Bytes())
r := bytes.NewReader([]byte(`{"height":1.5,"age":10}`))
if err := json.NewDecoder(r).Decode(s); err != nil {
panic(err)
}
fmt.Printf("value: %+v\n", s)
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.