Golang,函数内部的构建错误

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

Golang, build error inside function

问题

我有一个非常令人沮丧的构建错误,我已经盯着它看了一两个小时。它涉及到我链表程序中的一个函数。它认为我在函数外部有语句,但它们明显是在函数内部,并且认为 { : } 的比例不正确。我是不是漏掉了一些非常简单的东西?

// Index函数返回元素e的位置。如果e不存在,则返回0和false;否则返回位置和true。
func (list *linkedList) Index(e AnyType) (int, bool) {
var index int = 0
var contain bool = false
if list.Contains(e) == false {
return 0, false
}
for int i := 0; i < list.count; i++ { \175
list.setCursor(i)
if list.cursorPtr.item == e {
index = list.cursorIdx
contain = true
}
}
return index, contain \182
} \183

构建错误

./lists.go:175: 语法错误:意外的名称,期望 {
./lists.go:182: 非声明语句在函数体外
./lists.go:183: 语法错误:意外的 }

感谢任何帮助。谢谢。

英文:

So I have a really frustrating build error I have been staring at for the past hour or two. It involves one of my functions in my linked list program. It thinks I have statements outside the function when they are clearly inside, and thinks the { : } ratio is off. Am I missing something really simple?

// Index returns the location of element e. If e is not present,
// return 0 and false; otherwise return the location and true.
func (list *linkedList) Index(e AnyType) (int, bool) {
        var index int = 0
        var contain bool = false
        if list.Contains(e) == false {
            return 0, false
        }
        for int i := 0; i &lt; list.count; i++ {    \5
            list.setCursor(i)
            if list.cursorPtr.item == e {
                index = list.cursorIdx
                contain = true
            }
        }
        return index, contain    \2
}    \3

Build errors

./lists.go:175: syntax error: unexpected name, expecting {
./lists.go:182: non-declaration statement outside function body
./lists.go:183: syntax error: unexpected }

I appreciate any help. Thank you.

答案1

得分: 4

看起来问题出在第175行,应该是这样的:

for i := 0; i < list.count; i++ {

注意我去掉了 int

英文:

Looks like it's all line 175's fault, should be

for i := 0; i &lt; list.count; i++ {

note I removed int

huangapple
  • 本文由 发表于 2013年10月12日 05:02:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/19327202.html
匿名

发表评论

匿名网友

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

确定