Home>Article>Backend Development> Does go language require semicolon?
The Go language does not require a semicolon at the end of a statement or declaration unless there are multiple statements on a line. By default, one line of Go language is one piece of data. The compiler will actively convert the newline character after a specific symbol into a semicolon. Therefore, the position of the newline character added will affect the correct parsing of the Go code.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
The Go language does not require a semicolon at the end of a statement or declaration unless there are multiple statements on a line. In fact, the compiler will actively convert the newline character after a specific symbol into a semicolon, so the position of the newline character will affect the correct parsing of the Go code. A string literal, one of the keywords break , continue , fallthrough , or return , an operator and delimiter, one of -- , ) , ] , or } ).
For example, the left bracket { of the function must be on the same line as the func function declaration, and located at the end, and cannot occupy a separate line.In the expression x y, it can be wrapped in a new line after, but cannot be in Before the newline (Translation note: If it ends with, the semicolon delimiter will not be inserted, but if it ends with x, the semicolon delimiterwill be inserted, resulting in a compilation error.
Code style of Go language
(1) Indentation and line breaks
Indentation can be formatted directly using the gofmt tool (gofmt is Use tab indentation)
In terms of line breaks, the maximum length of a line should not exceed 120 characters. If it exceeds, please use line breaks to display, and try to keep the format elegant
We use GoLand development tools, use it directly Shortcut key: Ctrl Alt L.
(2) The end of the statement
In the Go language, there is no need to end with a semicolon similar to Java, which is the default line. A piece of data.
If you plan to write multiple statements on the same line, they must be used.
(3) Brackets and spaces
In terms of brackets and spaces, you can also directly use the gofmt tool to format (go will force the left brace not to wrap, and wrapping will report a syntax error). Spaces must be left between all operators and operands. [Related recommendations:Go video tutorial】
//正确的方式 if a > 0 { } //错误的方式 if a>0 // a,>,0之间应该使用空格 { //左大括号不可以换行,会报语法错误 }
(4) Import specifications
In the case of multiple lines of import, goimports will automatically format it for you. If you are A package is introduced in a file. It is recommended to use the following format:
import { "fmt" }
If your package introduces three types of packages, standard library packages, program internal packages, and third-party packages, it is recommended to organize them in the following way. Your package
inport{ "encoding/json" "strings" "myproject/models" "myproject/controller" "github.com/astaxie/beego" }
Introduces packages in order, with different types separated by spaces. The first is the actual quasi-library, the second is the project package, and the third is the third-party package
in the project Do not use relative paths to introduce packages
(5) Error handling
The principle of error handling is that any call that returns err cannot be discarded. Do not use _discard, all must be processed. When receiving an error, either return err, or use log to record it
Return as soon as possible: Once an error occurs, return immediately
Try not to use panic unless you know what you are doing
If the error description is in English, it must be in lowercase, and there is no need for punctuation at the end
Use an independent error stream for processing
// 错误写法 if err != nil { // error handing } else { //normal code } // 正确写法 if err != nil { // error handing return // or continue, etc. } // normal code
(6) Test
The unit test file naming convention is example_test.go
The function name of the test case must start with Test
Every important function must first write a test case, and submit the test case together with the regular code to facilitate regression testing
For more programming-related knowledge, please visit:programming video! !
The above is the detailed content of Does go language require semicolon?. For more information, please follow other related articles on the PHP Chinese website!