gpt4 book ai didi

java - 在Golang中初始化一个新类(Convert Java to Golang)

转载 作者:数据小太阳 更新时间:2023-10-29 03:44:13 25 4
gpt4 key购买 nike

我正在尝试将此 java 转换为 golang,但现在我遇到了这个错误。我不知道为什么会出现这个错误。

这是Java代码:

ArrayList<Cell> path; // path does not repeat first cell
String name;
static int count = 0;

public Path() {
this.path = new ArrayList<>();
this.name = "P" + (++this.count);
}

public Path(Path op) {
this.path = new ArrayList<>();
this.name = op.name;
path.addAll((op.path));
}

这是我写的

type Path struct {
name string
count int
path []Cell
}

func NewPath() (p *Path) {
p = new(Path)
p.path = []Cell{}
p.count = 0
p.name = "P" + strconv.Itoa(1+p.count)
return
}

func NewPath(op Path) (p *Path) {
p = new(Path)
p.path = []Cell{}
p.count = 0
p.name = op.name
p.path = append(p.path, op.path)
return
}

go 系统说我在重新声明 NewPath 方面是错误的,错误是:

prog.go:21:6: NewPath redeclared in this block

如何调试它?

最佳答案

Golang 不支持重载方法名。

您只需调用(其中一个)不同的方法。

关于java - 在Golang中初始化一个新类(Convert Java to Golang),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55082715/

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