初始化一个具有自定义方法的通用链表

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

Initialize a generic linked list with custom method

问题

我正在尝试使用通用数据类型实现一个链表,并创建了一个名为new()的方法,该方法创建一个具有nil根节点和长度为0的列表。现在的问题是如何在主函数中初始化该列表。

type Node[T any] struct {
	data T
	next *Node[T]
}

type LinkedList[T any] struct {
	root   *Node[T]
	length uint
}

func (list *LinkedList[T]) new() *LinkedList[T] {
	return &LinkedList[T]{root: nil, length: 0}
}
func main() {
	list := LinkedList[int].new()
}

我尝试的方法是这样的,但是会出现"cannot call pointer method new on LinkedList[int]"的错误提示。

英文:

I'm trying to implement a linked list with a generic data type and I created a method new() which creates a list with a nil root and length = 0. Now the problem is how do I initialize the list in the main function.

type Node[T any] struct {
	data T
	next *Node[T]
}

type LinkedList[T any] struct {
	root   *Node[T]
	length uint
}

func (list *LinkedList[T]) new() *LinkedList[T] {
	return &LinkedList[T]{root: nil, length: 0}
}
func main() {
	list := LinkedList[int].new()
}

This is what I tried to do which gives "cannot call pointer method new on LinkedList[int]".

答案1

得分: 2

func (list *LinkedList[T]) new() *LinkedList[T]

这是一个方法,它只会存在于已经初始化的LinkedList[T]实例上。所以将其作为一个方法并不是很有意义,因为你必须先创建一个LinkedList[T]实例,然后才能调用new方法来获取*LinkedList[T]

你可以这样做:

	l := &LinkedList[int]{}
	l = l.new()

但是由于new()只是将字段显式设置为它们的零值,你也可以这样做:

	l := &LinkedList[int]{}
英文:
func (list *LinkedList[T]) new() *LinkedList[T]

This is a method which will only exist on an already initialized instance of LinkedList[T]. So it doesn't make a ton of sense to do as a method as you'll have to create a LinkedList[T] to call new on it before you can get a *LinkedList[T]

You could do this:

	l := &LinkedList[int]{}
	l = l.new()

But since new() just explicity sets fields to their zero values, you ay as well just:

	l := &LinkedList[int]{}

答案2

得分: 1

erik258的答案是正确的。正如他在答案的最后部分提到的那样,由于零值是有效的初始值,你只需要使用结构体字面量初始化一个LinkedList。不需要构造函数或初始化函数。

然而,以防后来的任何人想知道如何为Go中的泛型类型编写初始化构造函数,我想我可以补充一下。

这里有一个关于构造函数主题的答案,其中提供了更多信息。它们也在Effective Go中提到。

在此答案的基础上,以下是你可以为你的问题中的泛型类型编写构造函数的方法:

func NewLinkedList[T any]() *LinkedList[T] {
   return &LinkedList[T]{root: nil, length: 0}
}

这段代码在Go Playground上运行。

英文:

The answer from erik258 is correct. As he mentioned in the final part of his answer, since the zero values are valid initial values, you should just initialize a LinkedList using a struct literal. No constructor or initializer function is needed.

However, just in case anyone who finds this question later is wondering how initializing constructors can be written for generic types in Go I thought I would add that.

Here's an answer with more info on the topic of constructors. They are also mentioned in Effective Go.

Building on that answer, here's how you can write a constructor for the generic type from your question:

func NewLinkedList[T any]() *LinkedList[T] {
   return &LinkedList[T]{root: nil, length: 0}
}

Here's that code running in Go Playground.

huangapple
  • 本文由 发表于 2023年3月9日 08:11:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75679333.html
匿名

发表评论

匿名网友

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

确定