golang error: 'expected ',' or '}' but got…” How to solve it?

PHPz
Release: 2023-06-24 14:42:19
Original
2764 people have browsed it

When writing code in Golang, you will often encounter various compilation errors. Among them, “expected ‘,’ or ‘}’ but got…” is a common error. It usually occurs when syntax errors in the code cause the compiler to fail to correctly identify where the code ends.

This article will discuss how to solve this error report from the following aspects.

  1. Check the matching of brackets

In Golang, each left bracket must have a corresponding right bracket. When we use parentheses in our code, we need to make sure they match correctly. If the brackets are not matched correctly, the compiler may not be able to parse the code correctly, resulting in an "expected ',' or '}' but got..." error.

For example, in the following code snippet, the brackets on the first line are not matched correctly, causing the compiler to fail to recognize the end of the code and report an error.

func main() {
    var a = [1,2,3]
    fmt.Println(a)
}
Copy after login

The correct code should be:

func main() {
    var a = [3]int{1,2,3}
    fmt.Println(a)
}
Copy after login
  1. Check the semicolon at the end of the statement

In Golang, every statement must end with a semicolon (;)Finish. If we miss a semicolon in the code, the compiler will not be able to correctly identify the end of the code, resulting in an error of “expected ‘,’ or ‘}’ but got…”.

For example, in the following code snippet, the semicolon is missing from the first line, causing the compiler to be unable to correctly parse the code and report an error.

var a int
var b int
a = 1
b = 2
fmt.Println(a+b)
Copy after login

The correct code should be:

var a int;
var b int;
a = 1;
b = 2;
fmt.Println(a+b);
Copy after login
  1. Check the parameters and return values ​​of the function

In Golang, each function has a set of parameters and a return value. If the format of parameters and return values ​​is incorrect when we define a function, the compiler will not be able to parse the code correctly and will report an error “expected ‘,’ or ‘}’ but got…”.

For example, in the following code snippet, the parameters and return values ​​of the function are in incorrect formats, causing the compiler to fail to parse the code correctly and report an error.

func main {
    fmt.Println("Hello World!")
}
Copy after login

The correct code should be:

func main() {
    fmt.Println("Hello World!")
}
Copy after login

4. Use the code editor

In order to avoid the errors discussed above, it is recommended to use the code editor to write Golang code. The code editor has functions such as automatic statement ending and automatic bracket matching, which can effectively help us avoid these errors.

Commonly used code editors include VScode, Sublime, etc. They all provide a wealth of extensions and plug-ins, which can greatly improve our coding efficiency.

Conclusion

In Golang, “expected ‘,’ or ‘}’ but got…” is a very common compilation error. We can solve this problem by checking for bracket matching, statement semicolons, function parameters and return values, etc. Using a code editor can also effectively avoid these errors. Golang is a very excellent programming language. Mastering the skills of this language will greatly improve our programming abilities.

The above is the detailed content of golang error: 'expected ',' or '}' but got…” How to solve it?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template