Go grammar Foundation

Posted by superdezign on Sun, 19 Dec 2021 19:32:38 +0100

Typical Go file layout

  1. package clause
  2. Any import statement
  3. Actual code
// Each Go file starts with a package clause, which means that all the remaining code in the file belongs to the "main" package
package main

// Go files almost always have one or more import statements
import "fmt"

// When the program runs, first run the "main" function
func main() {
    // The content is displayed on the terminal by calling the "Println" function from the "fmt" package
    fmt.Println("Hello, World!")
}

Files need to be imported into other packages before using the code contained in other packages.

Call function

To call a function, you need to enter the function name and a pair of parentheses.

A function can receive one or more parameters, which appear in parentheses after the function name, and multiple parameters are separated by commas.

character string

A string is a series of bytes, usually representing text characters.

Go treats the text between double quotes as a string.

In a string, line breaks, tabs, and other characters that are difficult to include in program code can be represented by escape sequences: a backslash followed by a character representing another character.

\n indicates a newline character
\t for tab
\"Indicates double quotation marks
\Indicates a backslash

fmt.Println("Hello, \nWorld!")
Hello,
World!
fmt.Println("Hello, \tWorld!")
Hello,  World!
fmt.Println("\"\"")
""
fmt.Println("\\")
\

Runes

The rune of Go is used to represent a single character.

String literals are surrounded by double quotation marks (''), and rune literals are surrounded by single quotation marks ('').

Go uses the Unicode standard to store rune s, which are saved as numeric codes rather than characters themselves.

Escape sequences can be used in rune literals.

fmt.Println('a')
97

Boolean value

Boolean values can only be one of two values: true or false.

number

Go treats integers and floating-point numbers as different types. You can use the decimal point to distinguish between integers and floating-point numbers.

operator

Arithmetic operator

Assignment Operators

Relational operator

Logical operator

type

Go is a static type. It knows what the value type is before the program runs. If the wrong value type is used in the wrong position, go will give an error message.

You can view the type through the TypeOf function of the reflect package:

fmt.Println(reflect.TypeOf(3.1415))
float64

Basic type

typelengthDefault valuedescribe
bool1false
byte10unit8
int,unit4,80The default integer type is to save numbers. 32 or 64
int8,unit810
int16,unit1620
int32,unit3240
int64,unit6480
float3240.0
float6480.0The default floating-point number type holds numbers with decimal parts.
complex648
complex12816
rune40
unitptr4,80
string ""

Declare variable

In Go, a variable is a piece of storage that contains values.

Use the var keyword, followed by the desired name and the type of value the variable will hold.

Variable declaration standard format:

var Variable name variable type

a. Post variable type
b. The variable naming rules follow the hump naming method
c. After a variable is declared, the system will automatically assign a zero value to this type
d. Variables declared but not used are treated as errors

var quantity int
// You can declare multiple variables of the same type at once
var length, width float64
var name string

initialize variable

After declaring a variable, you can assign it a value of this type using = (single equal sign)

quantity = 8
length, width = 1.2, 2.4
name = "hello go"

If you know what the value of the variable is, you can assign a value while declaring the variable (declare the variable and assign a value).

Variable initialization standard format

var Variable name variable type = expression
var quantity int = 8
var length, width float64 = 1.2, 2.4
var name string = "hello go"

You can assign new values to existing variables, but they must be values of the same type (the assigned type matches the declared type).

If you assign a value to a variable while declaring it, you can usually omit the variable type in the declaration, and the type of the value assigned to the variable will be used as the type of the variable.

Variable initialization derivation format

var Variable name = expression
var quantity = 8
var length, width = 1.2, 2.4
var name = "hello go"

Variable initialization short format

Variable name := expression
quantity := 8
length, width := 1.2, 2.4
name := "hello go"

Short variable declaration

  • In the function,: = concise assignment statement can be used to replace var definition where the type is explicit.
  • Every statement outside a function must start with a keyword (var, func, etc.): = structure cannot be used outside a function.

Variable scope

  • The global variable scope is the entire package
  • The scope of a local variable is determined by curly braces, and the scope of any variable is only within the curly braces

Zero value

If you declare a variable without assigning a value to it, the variable will contain the zero value of its type.

  • The zero value of the numeric type is 0
  • The zero value of string type is an empty string ''
  • The zero value of boolean type is false

Naming rules

Names applicable to variables, functions, and types:

  • The name must start with a letter and can consist of any number of additional letters and numbers.
  • If the name of a variable, function, or type begins with an uppercase letter, it is considered exported and can be accessed from a package other than the current package. If the name of a variable, function, or type begins with a lowercase letter, the name is considered exported and can only be used in the current package.
  • If a name consists of more than one word, it is recommended to name it in the form of hump.

transformation

The mathematical and comparison operations in Go require the same type of values. If the types are different, an error will be reported when running the code.

Conversion, which allows values to be converted from one type to another.

The conversion between numeric types (byte, run, intx, uintx, floatx) needs to be completed through type strong conversion

var price int = 2
// float64: type to be converted price: value to be converted
float64(price)
var a int32 = 123
var b int64 = int64(a)

The conversion between numeric type and string is performed through the functions provided in the standard library strconv

// string to int
int, err := strconv.Atoi(string)

// string to int64
int64, err := strconv.ParseInt(string, 10, 64)

// int to string
string := strconv.Itoa(int)

// int64 to string
string := strconv.FormatInt(int64, 10)

Go tool

Topics: Go