小编Nag*_* SV的帖子

在运行时以编程方式创建结构-可能吗?

Go中可以通过编程方式创建结构类型(即不在编译的源代码中)吗?

我们有一个特殊的用例,其中将通过用户定义的元数据创建类型(因此事先不知道模式/类型),并且每个客户都会有所不同。然后,我们需要为这些服务自动生成REST服务,并将其保留在NoSQL后端中。我们还需要为每个字段定义不同的动态验证器(例如,必填项,正则表达式,最大/最小大小,最大/最小值,对另一个类型实例的引用等)。

我想知道在Go中是否可以进行类似的操作?

编辑1:

例如

从JSON中的前端

For customer 1:
{
"id":1,
"patientid":1,
"name":"test1",
"height":"160",
"weight":"70",
"temp":"98"
}

For customer 2:
{
"id":2,
"patientid":1,
"name":"test1",
"height":"160",
"weight":"70"
}

For customer 3

may be different new fields will add
Run Code Online (Sandbox Code Playgroud)

后端

// For One customer need to have these fields 

type Vitalsigns struct {
    ID                int64  `datastore:"-"`
    PatientID         int64  `json:"patientid,omitempty"`
    Name              string `json:"name,omitempty"`
    Height            string `json:"height,omitempty"`
    Weight            string `json:"weight,omitempty"`
    Temp              string `json:"temp,omitempty"`
}



// Another need to have these fields

type Vitalsigns struct …
Run Code Online (Sandbox Code Playgroud)

reflection struct types go

1
推荐指数
1
解决办法
112
查看次数

标签 统计

go ×1

reflection ×1

struct ×1

types ×1