意外的分号或换行符,否则,即使之前也没有, [英] unexpected semicolon or newline before else even though there is neither before else if

查看:53
本文介绍了意外的分号或换行符,否则,即使之前也没有,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在golang代码中修复这些错误,如果有人可以帮助我解决问题,我将不胜感激.

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

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

尽管,最让我困扰的是第21行中的第一个错误,该错误指出:语法错误:意外的分号或换行符在其他位置之前.在第21行或之前根本找不到分号或新行.

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.

另外,第28行和第32行的错误是什么意思(函数主体外部的非声明语句)-这些语句在main()函数中,最后一个右括号关闭了该函数,所以为什么那里存在错误

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.

这是代码:

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

推荐答案

在Go中,您需要将'else'与右括号放在同一行.

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

Go插入一个;以某些标记结尾的行末,包括};请参阅规范.幸运的是,这意味着它可以在x:= func(){...}或x:= [] int {1,2,3}上插入结尾的分号,但这也意味着它在}结束后插入一个分号.您的if区块.由于如果{...} else {...}是单个复合语句,则您不能在第一个}之后加上分号,因此需要放置} else {一行

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.

这篇关于意外的分号或换行符,否则,即使之前也没有,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆