Go language basic syntax


In the previous chapter we have already understood the basic structure of the Go language. In this chapter we will learn the basic syntax of the Go language.


Go tags

Go programs can be composed of multiple tags, which can be keywords, identifiers, constants, strings, and symbols. For example, the following GO statement consists of 6 tags:

fmt.Println("Hello, World!")

The 6 tags are (one per line):

1. fmt
2. .
3. Println
4. (
5. "Hello, World!"
6. )

Line separator

in the Go program , one line represents the end of a statement. Each statement does not need to end with a semicolon ; like other languages ​​in the C family, because this work will be done automatically by the Go compiler.

If you plan to write multiple statements on the same line, they must be artificially distinguished by ;, but in actual development we do not encourage this practice.

The following are two statements:

fmt.Println("Hello, World!")
fmt.Println("w3cschoolphp中文网:w3cschool.cc")

Comments

Comments will not be compiled, and each package should have relevant comments.

Single-line comments are the most common form of comments. You can use single-line comments starting with // anywhere. Multi-line comments, also called block comments, start with /* and end with */. For example:

// 单行注释
/*
 Author by w3cschoolphp中文网
 我是多行注释
 */

Identifier

Identifier is used to name program entities such as variables and types. An identifier is actually a sequence of one or more letters (A~Z and a~z), numbers (0~9), and underscore_, but the first character must be a letter or underscore and not a number.

The following are valid identifiers:

mahesh   kumar   abc   move_name   a_123
myname50   _temp   j   a23b9   retVal

The following are invalid identifiers:

  • 1ab (begins with a number)

  • case (keyword of Go language)

  • a+b (operator is not allowed)


Keywords

The following lists 25 keywords or reserved words that will be used in Go code:

breakdefaultfuncinterfaceselect
casedefergo mapstruct
chanelsegotopackageswitch
constfallthroughifrangetype
continueforimportreturnvar

In addition to the keywords introduced above, the Go language also has 36 predefined identifiers:

appendbool bytecapclosecomplexcomplex64complex128uint16
copyfalsefloat32float64imagintint8int16uint32
int32int64iotalenmakenewnilpanicuint64
print printlnrealrecoverstringtrueuintuint8uintptr

Programs generally consist of keywords, constants, variables, operators, types and functions.

These delimiters may be used in the program: brackets (), square brackets [] and curly brackets {}.

These punctuation marks may be used in the program: .,,,;,: and ....


Spaces in Go language

The declaration of variables in Go language must be separated by spaces, such as:

var age int;

Appropriate use of spaces in statements can make the program easier to read. read.

No spaces:

fruit=apples+oranges;

Add spaces between variables and operators to make the program look more beautiful, such as:

fruit = apples + oranges;