我为使结构包含list.List与* list.List之间的意外差异而head之以鼻。为什么以下方法不起作用?
type listHolder struct {
id int
mylist list.List
}
func newListHolder(id int, text string) listHolder {
var newLH listHolder
newLH.mylist = *list.New()
newLH.id = id
newLH.mylist.PushBack(text)
return newLH
}
func (l *listHolder) pushBack(text string) {
l.mylist.PushBack(text)
}
func (l *listHolder) printAll() {
for temp := l.mylist.Front(); temp != nil; temp = temp.Next() {
fmt.Println(temp.Value)
}
}
func main() {
a := newListHolder(1, "first")
a.pushBack("second")
fmt.Printf("listHolder %d length %d Front()= %v, Back()=%v\n",
a.id, a.mylist.Len(), a.mylist.Front().Value, a.mylist.Back().Value)
a.printAll()
}
这将输出以下内容,表明长度是预期的,但是Front()和Back()方法不起作用。
listHolder 1 length 2 Front()= `<nil>`, Back()=<nil>
<nil>
如果我将结构定义为
// Same thing with a pointer
type listHolderPtr struct {
id int
mylist *list.List
}
func newListHolderPtr(id int, text string) listHolderPtr {
var newLH listHolderPtr
newLH.mylist = list.New()
newLH.id = id
newLH.mylist.PushBack(text)
return newLH
}
可以按预期工作,但是当然,listHolder结构的任何副本都共享对同一列表的引用,这不是我想要的。我需要能够复制周围的对象并获取内部列表的新副本。有可能吗?
请参见https://play.golang.org/p/KCtTwuvaS1R,以获取有关我要执行的操作的简化示例。在实际的用例中,我将在复杂的嵌套循环中将listHolder的一部分推到背面,并弹出每个listHolder的前面。
答案 0 :(得分:0)
我认为@JimB建议在每次将值推入listHolder.mylist
时都在列表中创建本地副本,这可能是解决您的问题的好方法(鉴于我正确理解了根本问题)。我试图提出一个类似于以下内容的实现:
package main
import (
"container/list"
"fmt"
)
type listHolder struct {
id int
mylist list.List
}
func newListHolder(id int) listHolder { // don't push back when constructing a new listHolder
var newLH listHolder
newLH.mylist = *list.New()
newLH.id = id
return newLH
}
func (l *listHolder) pushBack(text string) {
// create a temporary list to copy all old and the new value to
tmpList := list.New()
// copy all existing values from l.mylist
for e := l.mylist.Front(); e != nil; e = e.Next() {
fmt.Printf("pushing back '%v' from old list\n", e.Value)
tmpList.PushBack(e.Value)
}
// push back the new value
tmpList.PushBack(text)
// print the new tmpList for debugging purposes
for ele := tmpList.Front(); ele != nil; ele = ele.Next() {
fmt.Printf("creating new list element: %v\n", ele.Value)
}
// replace l.mylist with tmpList
l.mylist = *tmpList
// another version of this solution could be to return a new (i.e. copied)
// *listHolder with all the old values and the new 'text' value
}
func (l *listHolder) printAll() {
for temp := l.mylist.Front(); temp != nil; temp = temp.Next() {
fmt.Println(temp.Value)
}
}
func main() {
a := newListHolder(1)
a.pushBack("first") // push a value to a
a.pushBack("second") // push another value to a
fmt.Printf("listHolder %d length %d Front()=%v, Back()=%v\n",
a.id, a.mylist.Len(), a.mylist.Front().Value, a.mylist.Back().Value)
a.printAll()
}
此代码输出:
creating new list element: first // nothing to copy, only creating a new list element
pushing back 'first' from old list // copy element ...
creating new list element: first // ... from old list
creating new list element: second // and push new element to 'tmpList'
listHolder 1 length 2 Front()=first, Back()=second // print a summary
first // of the
second // new list
如果我有一些模拟数据,我可以做更多的测试/调试。至少这段代码无需使用*list.List
。