gpt4 book ai didi

go - 如何创建包装容器/list.List 的 golang 结构?

转载 作者:IT王子 更新时间:2023-10-29 01:14:29 25 4
gpt4 key购买 nike

让我的结构包含 list.List 与 *list.List 之间的意外差异令我抓狂。为什么以下不起作用?

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 的前面弹出。

最佳答案

我认为 @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 的情况下工作。

关于go - 如何创建包装容器/list.List 的 golang 结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50958063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com