wordSmith
wordSmith

Reputation: 3183

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

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

Upvotes: 15

Views: 18229

Answers (3)

Junior Grão
Junior Grão

Reputation: 1691

//=============
// Wrong way

if (yourCondition) {
  // ... 
} 
else if (yourElseIfCondition) {
  // ...
}

//=============
// Right way

if (yourCondition) {
  // ... 
} else if (yourElseIfCondition) { 
  // ...
}

See in the code https://go.dev/play/p/PyKok5AFxJ4

Upvotes: 1

Justin Lucas
Justin Lucas

Reputation: 1

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."); }

Upvotes: -1

twotwotwo
twotwotwo

Reputation: 30097

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.

Upvotes: 29

Related Questions