What is the basic knowledge of Go language
This article will explain in detail what the basic knowledge of Go language is, which the editor thinks is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1. The basic structure of Go program
The following is the basic structure of a Go program, including (package declaration, introduction package, function, etc.)
Package main
Import "fmt"
Func main () {
Fmt.Println ("Hello, World!")}
two。 Data type
Go built-in basic data types include basic data types and advanced data types
1. Basic data type
Boolean type (bool)
Numerical type
Integer type (byte,rune,int/uint,int8/uint8,int16/uint16,int32/uint32,int64/uint64)
Floating point type (float32,float64)
Plural type (complex64,complex128)
String type
String Go strings are concatenated by a single byte. Use UTF-8 encoding to identify Unicode text.
two。 Advanced data type
Array (array)
Slice (slice)
Dictionary (map)
Channel (channel)
Function (function)
Structure (function)
Interface (interface)
Pointer (* Xxx,Pointer,uintptr)
If divided according to the underlying structure, the value types include (all basic data types, arrays, structures), and the reference types include (slice,map,channel,function,interface, pointer)
3. Variable & constant
1. The variable name consists of letters, numbers, and underscores, in which the first letter cannot be a number, for example: var name string
two。 Statement
a. Specify the variable type, and if no value is assigned after declaration, use the default value.
Var name string
Name = "Li Si"
b. Determine the variable type according to the value.
Var name = "Li Si"
c. Short form, omitting var, note
Age: = 10
Be careful
(: =) is the preferred form for using variables
(: =) can only be used in the body of a function, not in the declaration and assignment of global variables.
(: =) the variable on the left should not have been declared, otherwise it will cause a compilation error.
3. Constant data types can only be Boolean, numeric (integer, floating-point, and plural), and string.
1. Constant declaration: const b string = "abc"
2. Iota, special constant
Const (
A = iota
B
C
)
1. Represents a continuous, untyped integer constant
two。 In constant declaration statements that begin with const
3. Increment once without assigning a constant, starting at 0
4. Once you cross a constant declaration statement that starts with const, it returns to 0.
4. Operator
1. Arithmetic operator, a + b, including (+, -, *, /,%, +, -)
two。 Relational operator, which returns True or False, a = = b, including (= =,! =, >, =