unexpected semicolon or newline before else even though there is neither before else if

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

unexpected semicolon or newline before else even though there is neither before else if

问题

我正在尝试修复我的 Golang 代码中的这些错误,如果有人能帮我解决,我会非常感激。

这是我的代码:http://play.golang.org/p/yELWfIdWz5

尽管如此,最困扰我的是第一个错误,出现在第21行,错误信息是:语法错误:else 前面的分号或换行符意外。我在第21行或其之前根本找不到分号或换行符。

另外,第28行和第32行的错误是什么意思(非声明语句在函数体外)- 这些语句都在 main() 函数中,并且最后的闭合大括号关闭了该函数,为什么会有错误呢?

我有一种感觉,所有这些错误都是由第一个错误引起的。

我非常感谢任何关于解决这些错误或至少了解更多信息的帮助。

以下是代码:

package main

import "fmt"

func main() {
    var current_mid = ""
    current_topic := make(map[string][]string)
    f, err := os.Open(*inputFile)
    if err != nil {
        fmt.Println(err)
        return
    }
    r := bufio.NewReader(f)
    xmlFile, _ := os.Create("freebase.xml")
    line, err := r.ReadString('\n')
    for err == nil {
        subject, predicate, object := parseTriple(line)
        if subject == current_mid {
            current_topic[predicate] = append(current_topic[predicate], object)
        } else if len(current_mid) > 0 {
            processTopic(current_mid, current_topic, xmlFile)
            current_topic = make(map[string][]string)
        }
        current_mid = subject
        line, err = r.ReadString('\n')
    }
    processTopic(current_mid, current_topic, xmlFile)
    if err != io.EOF {
        fmt.Println(err)
        return
    }
}

希望对你有帮助!

英文:

I'm trying to fix these errors in my golang code and if someone could help me with that, I'd appreciate it.

Here is my code: http://play.golang.org/p/yELWfIdWz5

Although, the one that is troubling me the most is the first one on line 21 where the error says: syntax error: unexpected semicolon or newline before else. I can't find a semicolon or new line on or just before line 21 at all.

Also, what do the errors on line 28 and 32 mean ( non-declaration statement outside function body )-- those statements are in the main() function and the last closing brace closes that function so why is there an error there.

I have a feeling that all of these errors are due to the first one.

I'd greatly appreciate any and all help in resolving these or at least learning more about them.

Here is the code:

package main

import "fmt"

func main() {
    var current_mid = ""
    current_topic := make(map[string][]string)
    f, err := os.Open(*inputFile)
    if err != nil {
	   fmt.Println(err)
	    return
    }
    r := bufio.NewReader(f)
    xmlFile, _ := os.Create("freebase.xml")
    line, err := r.ReadString('\n')
    for err == nil{
	    subject, predicate, object := parseTriple(line)
	    if subject == current_mid{
		    current_topic[predicate] = append(current_topic[predicate], object)
	    }
	    else if len(current_mid) > 0{
		    processTopic(current_mid, current_topic, xmlFile)
		    current_topic = make(map[string][]string)
	    }
	    current_mid = subject
	    line, err = r.ReadString('\n')
    }
    processTopic(current_mid, current_topic, xmlFile)
    if err != io.EOF {
	    fmt.Println(err)
	    return
    }
 }

答案1

得分: 25

在Go语言中,你需要将else与闭括号放在同一行。

Go语言在以}结尾的行末尾插入一个分号;,参见规范。这意味着它可以在x := func(){...}x := []int{1,2,3}这样的行末插入结束的分号,但它也会在关闭if块的}之后插入一个分号。由于if {...} else {...}是一个单一的复合语句,你不能在第一个}之后的中间插入分号,因此需要将} else {放在同一行上。

这种做法虽然不常见,但它使分号插入行为变得简单。在另一种可选分号的语言中,由于聪明的分号插入规则导致意外的程序行为,这种做法似乎还可以接受。

我可以理解错误消息可能会让人困惑,但是'newline before else'只是指前一行的}之后的换行符。

英文:

You need to put the 'else' on the line with the close brace in Go.

Go inserts a ; at the end of lines ending in certain tokens including }; see the spec. That means that, fortunately, it can insert the ending semicolon on x := func(){...} or x := []int{1,2,3}, but it also means it inserts one after the } closing your if block. Since if {...} else {...} is a single compound statement, you can't stick a semicolon in the middle of it after the first }, hence the requirement to put } else { on one line

It's unusual, but it keeps the semicolon-insertion behavior simple. Having been hit with unexpected program behavior because of the cleverer semicolon insertion rules in another semicolon-optional language, this seems alright in the scheme of things.

And I can see how the error message was confusing, but 'newline before else' just refers to the newline after the } on the previous line.

答案2

得分: -1

你可以在这里找到解释,但我觉得有点无关紧要。例如,这个看起来很不直观,但是可以编译通过:

if your_age >= 16 {
    say("\n你可以获得驾驶执照。")
} else if your_age >= 18 {
    say("\n你可以投票。")
} else {
    say("\n你可以玩得开心。")
}
英文:

https://golang.org/doc/effective_go.html#if

You can find the explanation here, but I find it to be a bit bikeshedding. For example, this, unintuitive as it is, compiles:

	if your_age >= 16 {
	say("\n You can earn a Drivers License."); } else
	if your_age >= 18 { say("\n You can vote."); } else
	{ say("\n You can have fun."); }

huangapple
  • 本文由 发表于 2014年10月15日 06:45:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/26371645.html
匿名

发表评论

匿名网友

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

确定