duanhu7390
2019-09-23 06:43
浏览 103
已采纳

Golang将任何结构存储在其他结构字段中

I have this code:

type TestData struct {
    Key string
}

type TemporaryStoreItem struct {
    key string
    data interface{}
    aliveUntil time.Time
}

func (s *TemporaryStoreItem) SetData(src interface{}) {
    src = s.data
}

data := TestData{
    Key: "value",
}

item := TemporaryStoreItem{
    key:     "item1",
    data:    data,
    aliveUntil: time.Now(),
}
oldItem := TestData{}
item.SetData(&oldItem)

I need to save any data or pointer to data in 'data' field of TemporaryStoreItem, and i need to get data as TestData struct(or any else source struct), what i do wrong, and is it possible to implement this idea?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题