GoLang:具有未知属性名称的结构的嵌套属性? [英] GoLang: Nested properties for structs with unknown property names?

查看:180
本文介绍了GoLang:具有未知属性名称的结构的嵌套属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用json从外部源获取一些值到结构中。使用UnMarshal将值放入一个结构中。

我有一个像UnMarshal这样的结构体:



<
类型字符串
值映射[字符串]接口{}
}
var数据帧

在UnMarshal之后,我可以通过访问类型: data.Type



但是如果我尝试这样做:

  if data。 Type =='image'{
fmt.Println(fmt.Sprintf(%s,data.Value.Imagedata))
}

编译器抱怨没有这样的值 data.Value.Imagedata



所以我的问题是,如何在代码中引用GoLang中的属性,我知道会在那里取决于某些条件?



这样做的工作原理:

 类型结构struct {
文件名字符串
}

类型结构struct {
类型字符串
值map [string] interface {}
}

但这不是很灵活,因为我会收到不同的 s

解决方案

UnMarshal将尽最大努力将数据放置在最适合您的结构的位置。从技术上讲,你的第一个例子可以工作,但你试图用点符号访问 Value 字段,即使你声明它是一个映射:

$ b $
$ b

如果data.Type =='image' {
fmt.Printf(%v \ n,data.Value [Imagedata])
}

......考虑到Imagedata是json中的一个关键。



您可以选择将结构定义为深度如你所希望的或期望的结构,或者使用接口{},然后在值上键入断言。使用Value字段作为地图,您总是可以访问像 Value [key] 这样的键,然后该地图的值就是您可以键入assert的接口{} Value [key]。(float64)



至于更明确的结构,我发现你可以将对象分解为自己的结构,或者将其定义在一个位置:

嵌套(带匿名结构)



 类型框架结构{
类型字符串
值结构{
Imagedata字符串`json:image_data`
}

$ / code>



单独结构



<$ p $类型字符串
值的值
}

类型的值struct {
Imagedata string`json: image_data`
}

我还在学习Go自己,所以这个程度我当前的理解: - )

I'm using json to get some values into a struct from an external source. Using UnMarshal to put the values in a struct.

I have a struct like this that UnMarshal puts values into:

type Frame struct{
Type string
Value map[string]interface{}
}
var data Frame

After the UnMarshal, I can access type by: data.Type

but if I try doing something like:

if data.Type == 'image'{
    fmt.Println(fmt.Sprintf("%s", data.Value.Imagedata))
}

The compiler complains about no such value data.Value.Imagedata

So my question is, how do I reference properties in GoLang in the code that I know WILL be there depending on some condition?

Doing this works:

type Image struct{
Filename string
}

type Frame struct{
Type string
Value map[string]interface{}
}

But that isn't very flexible as I will be receiving different Values

解决方案

The UnMarshal wil do its best to place the data where it best aligns with your struct. Technically your first example will work, but you are trying to access the Value field with dot notation, even though you declared it to be a map:

This should give you some form of output:

if data.Type == 'image'{
    fmt.Printf("%v\n", data.Value["Imagedata"])
}

... considering that "Imagedata" was a key in the json.

You have the option of defining the struct as deeply as you want or expect the structure to be, or using an interface{} and then doing type assertions on the values. With the Value field being a map, you would always access the keys like Value[key], and then value of that map is an interface{} which you could type assert like Value[key].(float64)

As for doing more explicit structures, I have found that you could either break up the objects into their own structs, or, define it nested in one place:

Nested (with anonymous struct)

type Frame struct {
    Type    string
    Value struct {
        Imagedata string `json:"image_data"`
    } 
}

Seperate structs

type Frame struct {
    Type    string
    Value   value 
}

type value struct {
    Imagedata string `json:"image_data"`
}

I'm still learning Go myself, so this the extent of my current understanding :-)

这篇关于GoLang:具有未知属性名称的结构的嵌套属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆