duanpuluan0480 2014-10-14 22:45
浏览 57
已采纳

意外的分号或换行符,如果没有

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('
')
    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('
')
    }
    processTopic(current_mid, current_topic, xmlFile)
    if err != io.EOF {
        fmt.Println(err)
        return
    }
 }
  • 写回答

2条回答 默认 最新

  • dongtan7351 2014-10-14 23:09
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog