douwen3198 2019-03-09 22:45
浏览 92
已采纳

在Golang中初始化新类(将Java转换为Golang)

I am trying to convert this java to golang and now I have this bug. I don't know why this bug is showing up.

here is the java code:

     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));
    }

Here is what i wrote

    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
}

The go system said I am wrong in term of redeclaring NewPath, with the error:

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

How can I debug it?

  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 vba代出现莫须有错误,都是一个个复制下来的为啥出错呢?
      • ¥15 /etc/vsftpd/vsftp.conf配置文件加了一行utf8_filesystem=YES之后,启动vsftpd报错
      • ¥15 W5100可以收广播,但是无法发出广播
      • ¥100 PCD点云排序和分割
      • ¥15 GG-CNN抓取数据集
      • ¥15 C++类和对象,多态性,继承,虚函数虚基类
      • ¥15 使用VS2019和Dev c++按%p输出地址,结果相差很大
      • ¥30 有偿解惑TINA老报错,求解惑
      • ¥15 arduino esp8266 编译问题
      • ¥15 metawrap画图报错