Golang 动态创建结构体成员

huangapple go评论93阅读模式
英文:

Golang dynamically creating member of struct

问题

我知道Go语言中有结构体(struct),但据我所知,你必须先定义结构体。我想知道如何声明一个在结构体中不存在的新变量。

type Circle struct{
    x, y, r float64
}

circle := new(Circle)
circle.color = "black"

你可以使用new关键字来创建一个结构体的实例,并且可以在实例中添加新的字段,比如color

英文:

I know there is struct in Go, but for all I know, you have to define 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"

答案1

得分: 62

你需要使用一个类型为map[string]interface{}的映射来处理动态JSON。下面是创建一个新映射的示例代码:

// 初始声明
m := map[string]interface{}{
    "key": "value",
}

// 动态添加子映射
m["sub"] = map[string]interface{}{
    "deepKey": "deepValue",
}

将JSON解组成映射的代码如下:

var f interface{}
err := json.Unmarshal(b, &f)

上述代码将在变量f中得到一个类似下面结构的映射:

f = map[string]interface{}{
    "Name": "Wednesday",
    "Age":  6,
    "Parents": []interface{}{
        "Gomez",
        "Morticia",
    },
}

你需要使用类型断言来访问它,否则Go无法知道它是一个映射:

m := f.(map[string]interface{})

你还需要对从映射中取出的每个项使用断言或类型判断。处理非结构化的JSON可能会很麻烦。

更多信息:

英文:

You will need to use a map (of type map[string]interface{}) to work with dynamic 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:

var f interface{}
err := json.Unmarshal(b, &f)

The code above would leave you with a map in f, with a structure resembling:

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:

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.

More information:

答案2

得分: 17

我已经开始在这个小仓库https://github.com/Ompluscator/dynamic-struct上工作。

目前可以通过传递一个结构体实例并修改字段(添加、删除、更改类型和标签)来在运行时扩展现有的结构体。

目前仍在进行中,所以不要期望有什么大的变化 Golang 动态创建结构体成员

编辑:目前,库的工作已经完成,并且在过去几个月中看起来很稳定 Golang 动态创建结构体成员

英文:

I've started to work on this small repository 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 Golang 动态创建结构体成员

EDIT: At this point, work on library is done, and it looks stable for last a couple of months Golang 动态创建结构体成员

答案3

得分: 7

你可以使用reflect包来实现,检查StructOf方法,它允许你根据[]reflect.StructField创建一个新的结构体。示例代码如下:

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)
}

以上是给出的代码示例,使用reflect包创建了一个新的结构体,并进行了一些操作。

英文:

You can do it using reflect package, check StructOf method it allows you to create a new struct from []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)

}

答案4

得分: 2

你无法这样做。Go语言是静态类型的,不允许这样的构造。

结构体在内存中有一个与定义直接相关的布局,没有地方存储这样的额外字段。

你可以使用map代替。此外,你可以使用&circle作为键或键的一部分,将map元素与任意结构体关联起来。

type key struct {
    target interface{}
    field string
}

x := make(map[key]string)
x[key{ target: circle, field: "color" }] = "black"
英文:

You can't. Go is statically typed, and does not allow such constructs.

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.

type key struct {
    target interface{}
    field string
}

x := make(map[key]string)
x[key{ target: circle, field: "color" }] = "black"

huangapple
  • 本文由 发表于 2016年11月12日 11:19:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/40559250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定