An unexpected semicolon or new line before

I am trying to fix these errors in my golang code, and if someone can help me, I would appreciate it.

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

However, the one that bothers me the most is the first on line 21, where the error says: syntax error: unexpected semicolon or new line before another. I cannot find a semicolon or a new line or immediately before line 21.

In addition to what the errors on lines 28 and 32 mean (an operator outside the declaration outside the function body) - these operators are in the main () function, and the last closing bracket closes this function, so why is there an error ?.

I have a feeling that all these mistakes are related to the first.

I would really appreciate any help in solving these problems, or at least 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 } } 
+6
source share
2 answers

You need to put "else" on the line with a closing bracket in Go.

Go inserts a; at the end of lines ending with specific tokens, including}; see specification . This means that, fortunately, it can insert the end semicolon in x: = func () {...} or x: = [] int {1,2,3}, but it also means that it inserts one after closing your if block. Since if {...} else {...} is the only compound statement, you cannot insert a semicolon in the middle of it after the first}, so the requirement is to put } else { on one line

This is unusual, but it keeps the behavior of the insert with a comma simple. Due to the fact that with the help of more complex rules for inserting a semicolon in another language with a semicolon, which was struck by unexpected programmatic behavior, this looks like a scheme of things.

And I see how the error message is misleading, but 'newline before else' just refers to the new line after} in the previous line.

+8
source

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

You can find an explanation here, but I think it's bit-biking. For example, this, unintuitive like this, 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."); } 
0
source

Source: https://habr.com/ru/post/976749/


All Articles