Like other programming languages, Go language projects (large programs) are written from basic components and basic syntax. We learned what Go looks like in the previous chapter. In this chapter, we will learn the basic syntax of Go, which will be an integral part of all Go programs.
1. Naming rules
The naming of types, variables, constants, statement labels, packages, and functions in Go follows a simple rule: Names begin with a letter or underscore, not a number, and can be followed by any number of characters, digits, and underscores, and are case-sensitive. For example, xcbeyond and xcbeyond are different identifiers.
The following are valid identifiers:
mahesh kumar abc move_name a_123
myname50 _temp j a23b9 retVal
Copy the code
The following is an invalid identifier:
1ab
: Starts with a numbercase
: Keyword of the Go languagea+b
The: operator is not allowed
2. Keywords
Go, like any other language, has some keywords for programs to use. There are 25 keywords or reserved words in Go:
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
Copy the code
In addition to the keywords described above, the Go language has 36 predefined identifiers:
append bool byte cap close complex complex64 complex128 uint16
copy false float32 float64 imag int int8 int16 uint32
int32 int64 iota len make new nil panic uint64
print println real recover string true uint uint8 uintptr
Copy the code
3, comments,
Comments, which enhance the readability of the code, do not participate in all functions of the program and will not be compiled. Every program should have comments in place to explain functionality, implementation, annotations, etc. If you don’t want others to spit on you, make sure you get in the habit of writing comments. Comments are just as important as code.
Go comments fall into two main categories:
- Single-line comments: You can use them anywhere
//
A single line comment at the beginning is the most common form of comment. - Multiline comment: also called block comment, to
/ *
Begin and end with* /
At the end.
As follows:
package main
import "fmt"
func main(a) {
// "Hello World!" Go language program
fmt.Println("Hello World!")
// This is a one-line comment
/* This is a multi-line comment by xcbeyond */
}
Copy the code
4. Line delimiters
In the Go program, a line represents the end of a statement. Each statement does not need a semicolon as in other languages such as Java; Because this is all done automatically by the Go compiler.
You must use this if you intend to write multiple statements on the same line; Artificial differentiation, but we don’t encourage it in real development.
Here are two statements:
fmt.Println("Hello, World!")
fmt.Println("Hello, xcbeyond!")
Copy the code
5. String output
Println or fmt.Print. Fmt. Println will Print a newline, similar to system.out. Println and System.out. Print in the Java language.
As follows:
package main
import "fmt"
func main(a) {
// Line wrap after output
fmt.Println("Xcbeyond, isn't it handsome?")
// No line breaks after output
fmt.Print("Handsome!")
fmt.Print("How handsome!")}Copy the code
Output result:
Xcbeyond, is it handsome? Handsome! Too handsome!Copy the code
In addition, string output can be formatted for output, as described in a later section.
String concatenation
The most common way to concatenate strings in Go is through + :
package main
import "fmt"
func main(a) {
fmt.Println("xc" + "beyond")}Copy the code
Output result:
xcbeyond
Copy the code
+ implement concatenation, will produce a new string to affect efficiency. Sprintf, strings.Join, and buffer.WriteString are also available, as described in the string section below.