转到 "unknown token"和 ": or newline expected"

标签 go

我在 GO 中实现了各种数据结构和算法,在我第一次运行 BST 时,我在这段代码中遇到了以下错误:

func insert(t Tree, k Node) {
    var newT Tree

    if t.root==nil {
        t.setRoot(k)
    }

    else if k.key < t.root.key {
        newT.setRoot(t.root.lc, newT)
        insert(newT,k)
    }
    else {
        newT.setRoot(t.root.rc, newT)
        insert(newT,k)
    }

}

行:

 // else is highlighted as an "unknown token" the bracket is ": or newline expected" and everything else is "unknown token"
    else {
        newT.setRoot(t.root.rc, newT)
        insert(newT,k)
    }

看看这个:

https://gobyexample.com/if-else

看起来这应该可行,所以我很困惑我在这里做错了什么

最佳答案

else 必须与 在同一行,因为 } 和行尾之间会自动插入分号。

func insert(t Tree, k Node) {
    var newT Tree

    if t.root == nil {
        t.setRoot(k)
    } else if k.key < t.root.key {
        newT.setRoot(t.root.lc, newT)
        insert(newT, k)
    } else {
        newT.setRoot(t.root.rc, newT)
        insert(newT, k)
    }
}

关于转到 "unknown token"和 ": or newline expected",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24740840/

相关文章:

go - 使用 io.WriteString 时可执行文件提前退出

go - "Bounded"在编程中的含义

sql - Golang SQL 查询变量替换

google-app-engine - 无法在我本地的 Google App Engine 上的 Go 中启动演示应用程序

string - 用空格解码base64

user-interface - 为什么找不到来自 andlabs/ui 包的组件

go - 对于回调,将 channel 作为数据通过其他 channel 传递

go - GOARCH 和 GOOS 在 go toolchain 编译过程中是如何使用的?

go - 从列表的大小循环,但在goroutine中未调用某些索引

go - 工作超过 slice 的末尾是惯用的吗?