MindMap Gallery [Community Work] GO Basic Grammar
Basic syntax of go language, with code examples. Summarizes basic syntax, types, built-in types, generics, etc. It’s full of useful information, friends in need should quickly collect it!
Edited at 2024-01-26 10:44:26Avatar 3 centers on the Sully family, showcasing the internal rift caused by the sacrifice of their eldest son, and their alliance with other tribes on Pandora against the external conflict of the Ashbringers, who adhere to the philosophy of fire and are allied with humans. It explores the grand themes of family, faith, and survival.
This article discusses the Easter eggs and homages in Zootopia 2 that you may have discovered. The main content includes: character and archetype Easter eggs, cinematic universe crossover Easter eggs, animal ecology and behavior references, symbol and metaphor Easter eggs, social satire and brand allusions, and emotional storylines and sequel foreshadowing.
[Zootopia Character Relationship Chart] The idealistic rabbit police officer Judy and the cynical fox conman Nick form a charmingly contrasting duo, rising from street hustlers to become Zootopia police officers!
Avatar 3 centers on the Sully family, showcasing the internal rift caused by the sacrifice of their eldest son, and their alliance with other tribes on Pandora against the external conflict of the Ashbringers, who adhere to the philosophy of fire and are allied with humans. It explores the grand themes of family, faith, and survival.
This article discusses the Easter eggs and homages in Zootopia 2 that you may have discovered. The main content includes: character and archetype Easter eggs, cinematic universe crossover Easter eggs, animal ecology and behavior references, symbol and metaphor Easter eggs, social satire and brand allusions, and emotional storylines and sequel foreshadowing.
[Zootopia Character Relationship Chart] The idealistic rabbit police officer Judy and the cynical fox conman Nick form a charmingly contrasting duo, rising from street hustlers to become Zootopia police officers!
GO basic syntax
basic grammar
main function
package declaration
Can be different from the name directory
The declaration of the test file can be added with the _test suffix
The declarations of .go files in the same directory must be consistent, and the declarations of test files must also be consistent.
Accessibility
Capitalized means it can be accessed outside the package.
Lowercase can only be accessed within the package
basic type
int family (can express positive and negative numbers)
int
The number of bytes used by int depends on the CPU
int8
1 byte
int16
2 bytes
int32
4 bytes
int64
8 bytes
unit family (unsigned integer) (can only express positive numbers)
uint
How many bytes are used by uint depends on the CPU
uint8
1 byte
uint16
2 bytes
uint32
4 bytes
uint64
8 bytes
float family
float32
4zijie
float64
8 bytes
string
For splicing
Auxiliary usage using strings package
The double quotes "" inside Println() need to be escaped, otherwise it will cause output errors. When escaping, you can write the content to be output and then copy it into the double quotes, so that the compiler can automatically escape
Println() can also use backticks ` `, and newlines can be added inside the backticks.
When splicing strings, you cannot connect numbers. You need to convert the numbers into strings first and then splice them. Use strconv.Itoa(123) to convert 123 into a string.
What you get using the function len() is the byte length of the string, not the number of characters. Use utf8.RuneCountInString() to get the number of characters
byte type
It is bytes, essentially uint8, which is also used to represent ASCII codes. The corresponding operations are in the bytes package.
[ ]byte slices can be converted to and from strings.
var a [ ]byte = 'a' When assigning a value using single quotes, you can only use a single character. At this time, the ASCII code value of a is 97.
var a [ ]byte = "hello" can be assigned to a string when using double quotes
bool type
And: (a && b)
OrOr: ( a || b)
Negation: (!a)
computation
The math package has more auxiliary uses
Hold down the command and click on the package function to jump into the package to see the relevant explanations.
Both sides of the operation must be of the same type. Int and int64 cannot be operated together and must be converted to the same type before operation.
Integers and integer operation results are also integers. If you want to get accurate values, you need to convert them to floating point types and then calculate.
variable
var a int = 123
a := 123
declare and assign
GO type inference
Can only be used for local variables
constant
const a int = 123
You can infer it yourself without the type.
Cannot modify
iota usage
Control constant initialization
However, using iota’s mathematical operations, including displacement operations
function
definition
keyword func
Function name
Whether the first letter is capitalized determines whether the package can be accessed
parameter list
Parameters can have names or not.
Parameters of the same type that are close together can declare their types together.
return value
Allow multiple return values
The return value can have a name, and the scope of the name is the entire function
recursion
A function calls itself internally
If the recursion is nested too deeply, stack overflow may occur.
Solved by increasing the size or modifying the code
Getting Started with Functional Styles
function as variable
The function itself can be assigned to a variable, and this variable can directly initiate a call
local function
Within function a, you can declare a function b. The scope of this function b is only within function a.
function as return value
Note: It is not the return value of the function, but the return value of the function itself, which is also an application of closure.
anonymous function
Closure
Interview highlights
context function
Context: defined outside the function
When an object is referenced by a closure, it will not be garbage collected. Improper use of closures can cause memory leaks.
functions as types
myFunc4 is actually a variable with type func and is assigned a value by Func3. Note that there is no () after Func3
indefinite parameters
last parameter
Equivalent to using slices
defer
First in, last out
Principles for determining values
Passed as a parameter: determined when defining
Introduced as a closure: determined when used
Modify return value
Only named return values can be modified
Implementation principle
Executed just before the function returns, called: delayed call
Functions with receivers are also called methods
control structure
if
if
if else
You can define a new local variable in the if else block
if else if
for
for range loop
Used to traverse arrays, slices, and maps
for i loop
for condition loop
switch statement
There can be a default branch
No need to break
Compilation can be switched through instructions
built-in types
array
Syntax [cap]type
Initialize to specify length
Can't change length
Can be initialized directly
arr[i] access element
slice
Syntax [ ]type
initialization
direct initialization
make initialization
Pay attention to estimated capacity
arr[i] access element
len gets the length
cap gets capacity
subslice
arr[start:end]
arr[start:]
arr[:end]
Does it contain data?
Subslice and slice memory sharing issues
Taking expansion as the criterion
Without expansion, they still share the same array.
After expansion, it is no longer the previous array.
map
initialization
direct initialization
make initialization
Estimated capacity
read
val,ok = m[key]
val value
ok, does key exist?
val = m[key]
If it exists, it is the output value
If it does not exist, a zero value of the corresponding type is output.
len gets the length
for key,val
The traversal results are different. k and v are in one-to-one correspondence, but the order is not certain.
delete delete
comparable concept
Generics
func A[T any]()
function generics
type A[T any] struct{}
Structure generics
type A [T any] interface{}
Generic interface
type constraints
type
interface type X interface{}
Define interface
An interface is an abstraction of behavior
Structure type X struct{}
initialization
Struct{}
&Struct{}
new(Struct)
Field assignment
Assign values in order
Assign value by name
self-reference
Only pointers can be used
Derived type type A B
A is a brand new pointer
Type alias type A = B
pointer
&Get address
* Dereference
method receiver
structure receiver
pointer receiver
Use pointers when in doubt
duck type
combination
There is no polymorphism in composition
A structure referencing a structure is a combination
Slices can be expanded
Naming rules
Capitalization controls accessibility
camel case nomenclature
Support type inference
Recommended Use