• Tags: backend, Golang

Go Token (Token)

The Go program consists of multiple tags.

These identifiers are keywords, identifiers, constants, strings, or symbols.

The following Go program statement consists of six tags:

fmt.Println("Hello, World!")
Copy the code

They are:

  • fmt
  • .
  • Println
  • (
  • Hello, World!
  • )

Line delimiters and line breaks

In the Go program, the end of each line (a line with a full statement) represents the end of a statement.

However, if the statement is incomplete, such as if (no corresponding) or [no corresponding], it is not considered to be the end of the sentence. For instance:

fmt.Println(  // This is not the end of an 🍊
    "Hello, World!") // the end of 🍊
fmt.Println("I'm from Guangzhou China 🇨🇳") // the end of 🍊
Copy the code

We don’t need to artificially give statements; Conclusion (unlike C++, C, Java, JavaScript), because all of this is done automatically by the Go compiler (which, like Kotlin, can be omitted or troublesome).

If you intend to write multiple statements on the same line, they must be added between them; A separator, such as:

fmt.Println("Hello, World!"); fmt.Println("I'm from Guangzhou China 🇨🇳") // the end of 🍊
Copy the code

However, for the sake of readability and cleanliness of the code, it is better to separate. One step to stomach pie or?)

annotation

Compilers ignore them, but using annotations efficiently can help us refactor and maintain future programs — or at least keep our eyes open. They start with /* and end with */, or start with //, like this:

/* Frost feather is so cute */
// I don't believe it!
Copy the code

The annotations in Go are edible in the same way as JavaScript, C++, Java and other languages, so penguins can eat them with confidence

identifier

Identifiers in Go are specifically used to identify the name of a variable, function, or any other user-defined item. Go identifiers begin with A letter (A to Z or A to Z) or underscore _, followed by zero or more letters, underscores, or digits (0 to 9).

But Go does not allow punctuation within identifiers, such as @, $, and %. Go is a case-sensitive programming language. Thus, Person and Person are two different identifiers in Go. Here are some identifiers that conform to the specification:

identifier identifier identifier identifier identifier
formHash username pwd available_state tmp_1
GitHubToken _tmp i j retVal
url k _formdata postdata retVal

It’s worth noting, though, that GoLand forces us to use Camel Case variable names instead of Snake Case:

GoLand you are bad!!

naming

Review the naming methods, there are five (four) basic:

  • camelCase
  • PascalCase
  • snake_case
  • kebab-case
  • whatisthiscase

Concatenation of strings

In Go we use + to concatenate two strings:

day3/string-concat.go

package main

import "fmt"

func main(a) {
    fmt.Println("Hoarfroster" + " is Handsome")}Copy the code

The keyword

Here are 25 keywords or reserved words in the Go code:

The keyword The keyword The keyword The keyword The keyword
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

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

identifier identifier identifier identifier identifier identifier identifier identifier
append bool byte cap close complex complex64 complex128
copy false float32 float64 imag int int8 int16
int32 int64 iota len make new nil panic
print println real recover string true uint uint8

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

Programs may use these delimiters: parentheses (), brackets [], and braces {}.

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

Spaces in the Go language

Variable declarations in Go must be separated by Spaces, as in:

var age int
Copy the code

Proper use of whitespace in statements makes programs easier to read. If there are no Spaces, it’s a little too crowded, like Hell West:

fruit=apples+oranges;
Copy the code

Add Spaces between variables and operators to make the program look nicer.

fruit = apples + oranges;
Copy the code

Formatted string

Sprintf format a string and assign it to a new string:

day3/string-format.go

package main

import "fmt"

func main(a) {
	// %d indicates an integer number and %s indicates a string
	var stockcode = 123
	var enddate = "2020-12-31"
	var url = "Code=%d&endDate=%s"
	var targetUrl = fmt.Sprintf(url, stockcode, enddate)
	fmt.Println(targetUrl)
}
Copy the code