This is a creation in Article, where the information may have evolved or changed.
Knowing and doing is easy, knowing it is not difficult. First, in this article, we point out some common mistakes and avoid the debugging work that you don't care about in the later programming.
-
- Surplus Imports
- Naming is case sensitive
- Semicolon Branch
- Invalid semicolon
- Grammar and other issues
Surplus Imports
Create a new file, copy and execute the following content
Errprog1.go
package mainimport"fmt"import"os"//excessive - we are not using any function in this packagefunc main() { fmt.Println("Hello world")}
The output is:
prog.go:4andnotos
GoThe compiler Go is very strict with the program, and if you don't use it, don't have extra requests. In the code above, trying to introduce a os package, but in the code is not used, the Go compiler is strictly prohibited by such behavior. After you remove the fourth line of code, the program compiles and runs correctly.
Naming is case sensitive
Errprog2.go
package mainimport"fmt"func main() { fmt.println("Hello world")}
The output is:
prog.go:6cannot refer to unexported name fmt.printlnprog.go:6undefined: fmt.println
In the above code, the print function is written fmt.println not previously written fmt.Println . Golanguages are case-sensitive, so when programming, they are referenced and invoked strictly as defined. The following code is not correct:
"fmt"import"Fmt"Func main() {}Fmt.Printlnfmt.println
Semicolon Branch
If you have learned,,, and C C++ Java Perl so on, you should have noticed Go (at least in the previous code) that there is no requirement to add a semicolon at the end of the statement. In fact Go , in the language, a semicolon is automatically added to the end of a line. However, if you have two expressions in the same way, you need to split them with a semicolon display. Let's raise a chestnut:
Errprog3.go
package mainimport"fmt"func main() { fmt.Println("Hello world") fmt.Println("Hi again")}
Output:
prog.go:6errornameoror }
To solve the above problems, you can put the above two statements in two lines
Part of the Code
func main() { fmt.Println("Hello world") fmt.Println("Hi again")}
To illustrate the role of semicolons, we use the following methods to modify
package mainimport"fmt"func main() { fmt.Println("Hello world"); fmt.Println("Hi again")
Output:
Hello worldHi again
Therefore, in a Go language, semicolons can be saved, and if they must be used, they are added without errors. Therefore, the following code is also correct drop.
Errprog4.go
package main;import"fmt";func main() { fmt.Println("Hello world"); fmt.Println("Hi again");};
The output is:
Hello worldHi again
But also please note these automatically generated semicolon.
Invalid semicolon
Continue to modify the above code
package mainimport"fmt";;func main() { fmt.Println("Hello world")}
The output is:
prog.go:3empty top-level declaration
The reason for the above problem occurs in the import following second semicolon, the first semicolon is correct, the above code has been verified. The second semicolon does not have any valid expressions before it, so the compiler reported the above error. Remove the extra semicolon and the program will run correctly.
Grammar and other issues
Each language has its own syntax requirements, and the Go compiler is no exception. Most of the time we make some grammatical mistakes, and we will list some of them for your reference.
Translator Note: The following error output is not translated, to keep the error output of the original better, this should not be a problem for everyone
Package ' main ' //error-no Quotes for the package name:package main Package "Main" //error-no Quotes for the package:package main PackageMain.x//error-packages names in Go is just one expression. so the either package is main or package X. Packagemain/x//error-packages names in Go is just one expression. so the either package is main or package X.Import ' FMT ' //error-needs double quotes "FMT"ImportFmt//error-needs double quotes "FMT"funcMain {}//error-functions has to is followed by Parantheses:func main () {}funcMain () []//error-where curly braces is required, only those is allowed. They is used to contain blocks of code. Func main () {}funcMain () {FMT. Println (' Hello World ') }//error-use double quotes for Strings:func main () {FMT. Println ("Hello World")}