在结构体中向interface{}添加数据。

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

Add data to interface{} in struct

问题

我想在main()函数中的fmt.Println(s)下面,将数据{"foo2": "bar2"}添加到结构体sample中的data中。

脚本

type sample struct {
    data interface{}
}

func (s *sample) func1() {
    obj := map[string]interface{}{}
    obj["foo1"] = "bar1"
    s.data = obj
}

func main() {
    s := &sample{}
    s.func1()
    fmt.Println(s)

    // 上面是这个问题的固定条件。
    // 在这里,我想将{"foo2": "bar2"}添加到s.data中。

}

我的测试

我尝试了以下方法。

测试1:

s.data["foo2"] = "bar2"

出现错误,提示type interface {} does not support indexing

测试2:

obj := map[string]interface{}{}
t, _ := json.Marshal(s.data)
json.Unmarshal(t, &obj)
obj["foo2"] = "bar2"
fmt.Println(obj)

没有错误。可以成功添加{"foo2": "bar2"}

这个第二个测试方法有效吗?如果有直接添加数据或其他方法,请告诉我。非常感谢你的时间。对于我的问题不成熟,我很抱歉。

英文:

I want to add a data {"foo2": "bar2"} to data in struct sample at below fmt.Println(s) in main().

Script

type sample struct {
	data interface{}
}

func (s *sample) func1() {
	obj := map[string]interface{}{}
	obj["foo1"] = "bar1"
	s.data = obj
}

func main() {
	s := &sample{}
	s.func1()
	fmt.Println(s)

    // Above here is fixed as a condition of this question.
	// Here, I want to add {"foo2": "bar2"} to s.data

}

My tests

I tried below.

Test 1:

s.data["foo2"] = "bar2"

Error occurs. It's type interface {} does not support indexing.

Test 2:

obj := map[string]interface{}{}
t, _ := json.Marshal(s.data)
json.Unmarshal(t, &obj)
obj["foo2"] = "bar2"
fmt.Println(obj)

There are no errors. {"foo2": "bar2"} can be added.

Is this 2nd test effective or general method? If there are how to directly add data or other methods, will you please teach me.
Thank you so much for your time. And I'm sorry for my immature question.

答案1

得分: 1

我不知道你为什么要使用空接口类型而不是map[string]interface{}类型,但是这是可以实现的。

type sample struct {
    data interface{}
}

func (s *sample) func1() {
    obj := map[string]interface{}{}
    obj["foo1"] = "bar1"
    s.data = obj
}

func (s *sample) addField(key, value string) {
    v, _ := s.data.(map[string]interface{})
    v[key] = value
    s.data = v
}

func main() {
    s := &sample{}
    s.func1()
    fmt.Println(s)

    s.addField("foo2", "bar2")
    fmt.Println(s)
}

每次想要添加一个值时,你需要将接口转换为map[string]interface{}类型。同样,你也可以使用我提供的方法来检索值。

我建议不要这样做。要么你始终使用map[string]interface{}类型,将其作为data的类型,要么你必须小心处理,因为不能保证你拥有的是map[string]interface{}类型,所以你必须相应地处理。

英文:

I don't know for what reason you want to have an empty interface type instead of the map[string]interface{} type, but it can be done.

type sample struct {
    data interface{}
}

func (s *sample) func1() {
    obj := map[string]interface{}{}
    obj["foo1"] = "bar1"
    s.data = obj
}

func (s *sample) addField(key, value string) {
	v, _ := s.data.(map[string]interface{})
	v[key] = value
	s.data = v
}

func main() {
    s := &sample{}
    s.func1()
    fmt.Println(s)
    
    s.addField("foo2", "bar2")
    fmt.Println(s)
   
}

You have to cast the interface into a map[string]interface{} each time you want to add a value. This can also be done to retrieve a value, I'm sure you can add another method using what I provided to do that.

I would advise against doing this. Either you'll always have a map[string]interface{}, so you should make that the type of data, or you'll have to be careful as there is no guarantee that you have a map[string]interface{} so you'll have to handle this accordingly.

答案2

得分: 0

如果你将结构体的定义更改为:

type sample struct {
    data map[string]interface{}
}

它应该可以工作。

为什么呢?因为你将data定义为interface{}。但是这不是一个map,它是一个空接口。为什么它不是一个map呢?因为它也可以是int、string或另一个结构体。所以你不能像其他类型一样使用这种类型。

在你的示例中,你总是将data用作map,所以解决方案就是将data定义为类型map[string]interface{}

英文:

If you change the definition of your struct to:

type sample struct {
    data map[string]interface{}
}

it should work.

So why? Because you are defining data as interface{}. But you this is not a map it is an empty interface. Why it is not a map? Because it could be also an int, string or another struct. So you can not use that type like other types.

In your examples you are using data always as a map, so the solution would be to define data as the type map[string]interface{}

huangapple
  • 本文由 发表于 2017年5月21日 15:35:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/44094325.html
匿名

发表评论

匿名网友

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

确定