我试图了解如何在Go中操作数据结构,以及它的指针方法(带有副本或引用)。
我的代码在Go Playground,这里:https://play.golang.org/p/j_06RS5Xcz
我创建了一个结构体切片的映射,该结构体内部也有其他东西的切片。
:
type Item struct {
Name string
Description string
}
type Entity struct {
Base Item
Others []Item
}
var database map[int][]Entity
func main() {
database = make(map[int][]Entity)
database[1] = []Entity{}
e1 := Entity{}
e1.Base = Item{"A", "aaa"}
e1.Others = []Item{}
database[1] = append(database[1], e1)
// later, I want to add other items to my entity
e1.Others = append(e1.Others, Item{"B", "bbb"})
// other items field is empty
fmt.Println(database)
}
// prints: map[1:[{{A aaa} []}]]
我想在程序后面附加Others项。看来我必须使用指针来解决这个问题,但我不知道怎么做。
我的实体应该是这样吗?
type Entity struct {
Base Item
Others *[]Item
}
,如果是,我应该如何给它附加项?像这样的吗?
*e1.Others = append(*e1.Others, Item{"B", "bbb"})
.
.
.
如果还有其他问题…我也不清楚我是否"必须"做:database[1] = []Entity{}
在database[1] = append(database[1], e1)
之前,或者我可以在这种情况下追加。我尝试了同样的e1.Others = []Item{}
,但它没有产生相同的效果追加(我知道这是我的误解,而不是围棋的错)。
提前感谢:)
在当前代码中,有两个类型为Entity
的对象。一个命名为e1
,另一个命名为database[1]
。这两个对象是完全独立的,因为它们是struct
。因此,当您更改其中一个时,不会影响另一个。(一个小例外:Items
字段的一些更改将被共享,但不是全部。)
如果您想先将实体添加到映射中,然后再修改它,则应该使用指针映射map[int][]*Entity
。然后,您应该使用e1 := &Entity{}
创建一个指向实体的指针,而不是Entity{}
,然后程序就可以工作了。对e1.Others
的更改也会影响database[1].Others
,因为这两个变量现在指向同一个对象。
但是print
语句是不同的。它将只打印指针值,而不是打印结构体。要解决这个问题,可以添加一个String方法:
func (e *Entity) String() string { return fmt.Sprint(*e) }
请参阅https://play.golang.org/p/edU7E5Gnjw,其中我还删除了不必要的空切片。添加到nil
切片是完全可以的。
进一步阅读,我建议http://research.swtch.com/godata,它将回答您目前的问题。