Go Language Basic Syntax

In the previous chapter, we learned the basic composition and structure of the Go language. In this chapter, we will learn the basic syntax of the Go language.


Go Tokens

A Go program can be composed of multiple tokens, which can be keywords, identifiers, constants, strings, and symbols. For example, the following Go statement consists of 6 tokens:

fmt.Println("Hello, World!")

6 tokens are (one per line):

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

Line Separator

In a Go program, a line represents the end of a statement. Each statement does not need to end with a semicolon ; like in other C-family languages, because these tasks are automatically completed by the Go compiler.

If you intend to write multiple statements on the same line, they must be manually separated with ;, but this practice is not encouraged in real development.

The following are two statements:

fmt.Println("Hello, World!")
fmt.Println("Example: example.com")

Comments

Comments are not compiled, and every package should have related 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, all start with /* and end with */. For example:

// 单行注释
/*
 Author by Example
 我是多行注释
 */

Identifiers

Identifiers are used to name program entities such as variables and types. An identifier is actually a sequence composed of one or more letters (A~Z and a~z), digits (0~9), and underscores _, but the first character must be a letter or an underscore and cannot be a digit.

The following are valid identifiers:

mahesh   kumar   abc   move_name   a_123
myname50   _temp   j   a23b9   retVal

The following are invalid identifiers:

  • 1ab (starts with a digit)
  • case (a Go language keyword)
  • a+b (operators are not allowed)

String Concatenation

String concatenation in Go can be done through+Implementation:

Example

package main
import "fmt"
func main() {
    fmt.Println("Google" + "Example")
}

The output of the above example is:

GoogleExample

Keywords

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

breakdefaultfuncinterfaceselect
casedefergomapstruct
chanelsegotopackageswitch
constfallthroughifrangetype
continueforimportreturnvar

In addition to the keywords introduced above, the Go language also 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

A program is generally composed of keywords, constants, variables, operators, types, and functions.

The program may use these delimiters: parentheses (), square brackets [], and curly braces {}.

The program may use these punctuation marks:.、,、;、:and…。


Whitespace in Go

In the Go language, spaces are usually used to separate identifiers, keywords, operators, and expressions, improving the readability of the code.

In Go, variable declarations must be separated by spaces, for example:

var x int
const Pi float64 = 3.14159265358979323846

Using spaces between operators and operands can make the program easier to read:

No spaces:

fruit=apples+oranges;

Adding spaces between variables and operators makes the program look more beautiful, for example:

fruit = apples + oranges; 

Spaces should be used between keywords and expressions.

For example:

if x > 0 {
    // do something
}

When calling a function, spaces should be used between the function name and the equals sign on the left, and also between parameters.

For example:

result := add(2, 3)

Format string

In Go language, usefmt.Sprintforfmt.PrintfFormat a string and assign it to a new string:

  • SprintfGenerate a formatted string according to the formatting parameters and return the string.
  • PrintfGenerate a formatted string according to the formatting parameters and write it to standard output.

Sprintf example

package main

import (
    "fmt"
)

func main() {
   // %d represents an integer, %s represents a string
    var stockcode=123
    var enddate="2020-12-31"
    var url="Code=%d&endDate=%s"
    var target_url=fmt.Sprintf(url,stockcode,enddate)
    fmt.Println(target_url)
}

The output result is:

Code=123&endDate=2020-12-31

Printf example

package main

import (
    "fmt"
)

func main() {
   // %d represents an integer, %s represents a string
    var stockcode=123
    var enddate="2020-12-31"
    var url="Code=%d&endDate=%s"
    fmt.Printf(url,stockcode,enddate)
}

The output result is:

Code=123&endDate=2020-12-31

For more content, see:

other extensions