This is the second chapter of learning go language, mind map is too big, in order to ensure its clarity, only part of it can be put out, the rest can only be described in words, like friends can like collection.
One, concurrent programming
1.1 basis
1.1.1 Processes, threads, coroutines
-
process
- Process is a program in the operating system in a process of execution, system allocation and scheduling of an independent unit
-
thread
- A thread is an execution entity of a process, the basic unit of CPU scheduling and dispatching, and a smaller unit that can run independently than a process
-
coroutines
- Independent stack space, shared heap space, scheduling controlled by the user, essentially similar to user-level threads, which also implement their own scheduling
1.1.2 Concurrency and parallelism
-
concurrent
- Multithreaded programs running on a core CPU are concurrent
-
parallel
- A multithreaded program running on a CPU with multiple cores is called parallelism
1.1.3 note
- The concurrency model of the Go language is CSP, which advocates communication through shared memory rather than through shared memory
1.2 goroutine
The Goroutine is scheduled and managed by the Go runtime, and the Go program intelligently allocates the tasks in the Goroutine to each CPU
1.2.1 use
-
To create a Goroutine for a function, simply prefix the call with the go keyword
-
The sample
-
Start a Goroutine
- Go XXX ()// Start another goroutine to execute XXX
-
Start multiple Goroutines
-
// Synchronize goroutine with sync.waitGroup
var wg sync.WaitGroup
func hello(i int) {
defer wg.Done() // When goroutine is finished, register -1
fmt.Println("Hello Goroutine!", i)
}
func main(a) {
for i := 0; i < 10; i++ {
wg.Add(1) // Start a goroutine and register +1
go hello(i)
}
wg.Wait() // Wait for all registered goroutines to finish
}
Copy the code
1.2.2 goroutine scheduling
GPM is the implementation of Go language runtime level and a scheduling system implemented by Go language itself.
1.2.2.1 GMP
- GMP model
- Meanings of each unit
-
G
- This is a Goroutine, which contains information about the goroutine and bindings to all P’s
-
P
- Manages a set of Goroutine queues. P stores the context (function pointer, stack address, and address boundary) in which goroutine is currently running. P will perform some scheduling on its own goroutine queue (such as suspending the Goroutine that occupies a long CPU time, running the subsequent goroutine, etc.). When its own queue is finished consuming, IT will fetch from the global queue. If the global queue is also finished consuming, it will go back to the queue of other P to grab tasks
-
M
- M (Machine) is the virtualization of the kernel thread of the operating system by the Go runtime. M and the kernel thread are generally mapped one by one. A Goroutine is finally executed on M.
- Design strategy of scheduler
-
The core
- Reuse threads: Avoid frequent creation and destruction of threads, but reuse threads
-
mechanism
-
The work mechanism of stealing
- When this thread has no running G, try to steal G from another thread’s bound P instead of destroying the thread
-
Hand off mechanism
- When this thread blocks on a system call due to G, the thread releases the bound P and transfers the P to another idle thread
-
- Explain in detail
- www.topgoer.com/%E5%B9%B6%E…
- The number of P and M
-
The number of P
- Determined by the boot-time environment variable GOMAXPROCS or by the Runtime method GOMAXPROCS(). This means that at any point in the program execution there is only GOMAXPROCS or the runtime method GOMAXPROCS(). This means that at any point in the program execution there is only GOMAXPROCS or the runtime method GOMAXPROCS(). This means that only GOMAXPROCS goroutines are running at any given time.
-
The number of M
- The limit of the GO language itself: When the GO program starts, the maximum number of M is set to 10000 by default, but the kernel can hardly support that many threads, so this limit can be ignored.
- The SetMaxThreads function in Runtime /debug sets the maximum number of METERS
- If an M is blocked, a new M is created
- When P and M are created
-
P to create
- Once the maximum number of P, n, is determined, the runtime system creates n ps from this number
-
M to create
- There is not enough M to associate P with and run the runnable G in it, for example, all M is blocked at this time, and P has many ready tasks and will look for idle M, and if there is no idle, it will create a new M
1.2.2.2 Go func() Scheduling process
1.2.2.3 note
-
M:N mapping between coroutines and threads
1.2.3 note
-
When the main coroutine exits, other coroutines also exit
-
Growable stack
- OS threads generally have a fixed stack memory (usually 2MB). A Goroutine stack starts its life with a very small stack (typically 2KB). The goroutine stack is not fixed and can be increased and decreased as needed.
-
In GO, the thread is the entity that runs the Goroutine, and the scheduler’s function is to assign runnable Goroutines to worker threads
-
In GO, one Goroutine consumes up to 10ms of CPU, preventing other Goroutines from starving to death
-
You can set up your own goroutine pool to effectively control the goroutine population
Characteristics of 1.
- Less memory footprint (several kilobytes)
- More flexible scheduling (Runtime scheduling)
1.3 Concurrency Security and Locking
1.3.1 background
In Go code, multiple Goroutines can operate on a resource at the same time (critical sections), and race issues can occur, so you need to keep it safe.
1.3.2 lock type
-
The mutex
- Mutex is a common way to control access to a shared resource, ensuring that only one Goroutine can access the shared resource at a time.
- use
// The go language uses the Mutex type of the sync package to implement Mutex. Var lock sync.mutex // Define the variable lock.lock () // Unlock lock.unlock () //Copy the code
-
Read and write mutex
- There are two types of read-write locks: read locks and write locks. When one goroutine acquires a read lock, the other Goroutine acquires a read lock and waits for a write lock. When one Goroutine acquires a write lock, the other Goroutines wait to acquire either the read lock or the write lock
- use
// Go uses the RWMutex type in the sync package to implement read and write locks. Var rwlock sync.rwmutex // Defines the Lock variable rwlock.lock () // adds a write Lock rwlock.unlock () // unlocks a write Lock Rwlock. RLock() // read lock rwlock.RUnlock() // read lockCopy the code
Two, common standard library
2.1 the runtime
2.1.1 function
Provides interoperability with the GO runtime environment
2.1.2 the commonly used
- runtime.Gosched()
Cause the current GO coroutine to abandon the processor to allow other GO coroutines to run. The current GO coroutine will resume execution in the future
- runtime.Goexit()
Terminate the go coroutine calling it, the other Go coroutines are unaffected, and Goexit() will execute all of the functions of defer before terminating the go coroutine
- runtime.GOMAXPROCS(n int)
Determines how many OS threads are required to execute the Go code simultaneously. The default is the number of CPU cores on the machine.
2.2 time
2.2.1 function
Provides a function for displaying and measuring time
2.2.2 the commonly used
- The Time related
(1)func Now() Time
Run the following command to obtain the current time
The commonly used
Month := now.month () // Day := now.day () // Hour := Timestamp1 := now.unix () // Get the timestamp. Timestamp1 := now.unix () // Get the timestamp Timestamp2 := now.unixnano () // Get the nanosecond timestampCopy the code
(2)func Unix(sec int64, nsec int64) Time
Unix(timestamp, 0) // Convert a timestamp to a time format; Unix time (from January 1, 1970 UTC to the time in seconds and nanoseconds)
(3) func (t Time) Equal(u Time) bool
Determine whether two times are the same (time zone is taken into account)
Func (t Time) Add(d Duration) Time
Return t+d
(5) func (t Time) Sub(u Time) Duration
Returns a time period t-u
(6) FUNC (t Time) Before(u Time) bool
Return true if t represents a point in time before u; Otherwise return false.
(7) func (t Time) After(u Time) bool
Return true if t represents a point in time after u; Otherwise return false.
(8)……
- Duration specifies the interval defined in the time package
Time. Duration is a type defined by the time package that represents the elapsed time between two points in time in nanoseconds.
constant
(1) time.Nanosecond or time.Duration // 1 Nanosecond
(2) time.microsecond // 1 subtlety
(3) time.millSecond // 1 millisecond
(4) time.Second // 1 Second
(5) time.Minute // 1 Minute
(6) Time.Hour // 1 Hour
- The Timer related
The Timer type represents a single time event. When a Timer expires, the time is sent to C unless the Timer was created by an AfterFunc function
operation
Func NewTimer(d Duration) *Timer
NewTimer Creates a Timer that will expire after at least d of the elapsed time.
(2) unc AfterFunc(d Duration, f func()) *Timer
AfterFunc waits for another go session d to pass and then calls f
(3) func (t *Timer) Reset(d Duration) bool
Reset causes t to restart the timer and wait for time segment D to expire. Returns true if t is still waiting when called; Returns false if t has expired or been stopped.
(4) FUNc (t *Timer) Stop() bool
Stop Stops the Timer execution.
- Ticker related
The Ticker keeps a channel and passes “tick” to it at regular intervals.
operation
(1) func NewTicker(d Duration) *Ticker
NewTicker returns a NewTicker that contains a channel field and sends the current time to the channel every time period d (executed multiple times when the time is up).
(2) func (t *Ticker) Stop()
Top Closes a Ticker. After the function is disabled, no more tick information will be sent.
- other
(1) func Sleep(d Duration)
Sleep blocks the current go for at least the period d represents. When d<=0, Sleep returns immediately.
Func After(d Duration) <-chan Time
After sends the time to the return value After another thread has passed the time period d. Equivalent to NewTimer(d).c.
Func Tick(d Duration) <-chan Time
The Tick is the encapsulation of the NewTicker and provides access only to the Ticker channel. This function is handy if you don’t need to close the Ticker.
2.3 the sync
2.3.1 function
The Sync package provides basic synchronization units, such as mutex
2.3.2 commonly used
-
type Mutex
- The mutex
-
type RWMutex
- Read and write mutex
-
type Once
Once is an object that performs an action only Once.
- Func (o *Once) Do(f func())// Do method executes function f if and only if it is called for the first time.
- type WaitGroup
WaitGroup is used to wait for the end of a set of threads. The parent thread calls the Add method to set the number of threads that should wait. Each thread being waited should call the Done method when it finishes. At the same time, the main thread can call the Wait method to block until all threads finish.
Method -Copy the code
(1) func (WG *WaitGroup) Add(delta int)
The Add method adds delta to the internal count
(2) func (WG *WaitGroup) Done()
The Done method decrement the WaitGroup counter and should be executed at the end of the thread.
(3) func (wg *WaitGroup) Wait()
The Wait method blocks until the WaitGroup counter is reduced to 0.
(4) Examples
var wg sync.WaitGroup
func hello(a) {
defer wg.Done()
fmt.Println("Hello Goroutine!")}func main(a) {
wg.Add(1)
go hello() // Start another Goroutine to execute hello
fmt.Println("main goroutine done!")
wg.Wait()
}
Copy the code
2.4 FMT
Against 2.4.1 function
The FMT package implements formatted I/O similar to the C languages printf and scanf. It is mainly divided into two parts: output content and input content
Commonly used 2.4.2
2.4.2.1 Outbound Output
- Print series
The Print series of functions will Print the content to the system’s standard output
(1) func Print(a… interface{}) (n int, err error)
Direct output
(2) func Printf(format string, a… interface{}) (n int, err error)
Supports formatted output strings
(3) func Println(a… interface{}) (n int, err error)
A newline character is added to the end of the output
- Fprint series
The Fprint series of functions will print the content to a variable w of type IO.Writer interface, which is usually used to write content to a file
Func Fprint(w IO.Writer, a… Interface {}) (n int, err Error) (2) func Fprintf(w IO.Writer, format String, a… Interface {}) (n int, err Error) (3) func Fprintln(w IO.Writer, a… interface{}) (n int, err error)
- Sprint series
The Sprint series of functions generates and returns a string from the incoming data
(1) Func Sprint(a… Interface {}) string (2) Sprintf(format String, a… Interface {}) string (3) Sprintln(a… interface{}) string
- func Errorf(format string, a … interface{}) error
The Errorf function generates a formatted string from the format argument and returns an error containing the string.
2.4.2.2 Obtaining Input
- Scan series
(1) Func Scan(a… interface{}) (n int, err error)
Scan scans text from standard input, reading values separated by whitespace characters and saving them to arguments passed to the function, with newlines treated as whitespace characters.
Func Scanf(format string, a… interface{}) (n int, err error)
Scanf scans text from standard input, reading whitespace delimited values in the format specified by the format argument and saving them to arguments passed to the function.
(3) func Scanln(a… interface{}) (n int, err error)
Scanln is similar to Scan in that it stops scanning only when a newline is encountered. The last data must be followed by a line break or end position.
- Fscan series
(1) func Fscan(r IO.Reader, a… interface{}) (n int, err error)
Fscan scans text from R, saving the whitespace delimited values that were successfully read into the arguments that were successfully passed to this function.
Func Fscanln(r IO.Reader, a… interface{}) (n int, err error)
Fscanln is similar to Fscan but stops scanning only when a line is wrapped.
Func Fscanf(r IO.Reader, format string, a… func Fscanf(r IO.Reader, format string, a… interface{}) (n int, err error)
Fscanf scans text from r and saves the whitespace delimited values that were successfully read in the format specified by the format argument that was successfully passed to this function.
- Sscan series
Func Sscan(STR string, a… interface{}) (n int, err error)
Sscan scans text from the string STR, saving the whitespace delimited value that was successfully read into the argument that was successfully passed to this function.
Func Sscanln(STR string, a… interface{}) (n int, err error)
Sscanln is similar to Sscan, but does not stop scanning until line feed. The last entry must be followed by a line break or end position.
Func Sscanf(STR string, format string, a… interface{}) (n int, err error)
Sscanf scans text from the string STR and saves the whitespace delimited value that was successfully read into the argument that was successfully passed to the function in the format specified by the format argument.
2.4.3 Formatting placeholders
-
Generic placeholders
The default format for the value of -% v indicates that - %+v is similar to %v, but the output structure will add the field name - %#v value of the Go syntax indicates that - %T printed value type - %% percent signCopy the code
-
The Boolean
-% t True or falseCopy the code
-
plastic
-% b is binary - %c The corresponding Unicode code value of this value - %d is decimal - % O is octal - %x is hexadecimal, a-f - %x is hexadecimal, and A-F - %U is Unicode format: U+1234, equivalent to "U+%04X" - %q The value corresponds to a single quoted go syntax character literal, safely escaped when necessaryCopy the code
-
Floating point and complex numbers
+ %e +78 - %E +78 - % F +78 - %f +78 - %f +78 - % F +78 - % F +78 - % F +78 - % F For example, 123.456 - %F is equivalent to %F - %g. The format of %E or %F is adopted according to the actual situation (for concise and accurate output). - %g Adopts the format of %E or %F according to the actual situation (for concise and accurate output).Copy the code
-
String and []byte
-% s Indicates the output string directly or []byte - %q Indicates the go syntax string literal enclosed in double quotation marks, which can be safely escaped if necessary. -% x Indicates that each byte is a two-character hexadecimal number (a-f indicates that each byte is a two-character hexadecimal number (A-F indicates that each byte is a two-character hexadecimal number)Copy the code
-
Pointer to the
-% p is expressed in hexadecimal, followed by a leading 0xCopy the code
-
Width identifier
- %f default width, default accuracy - %9f width 9, default accuracy - %.2f default width, accuracy 2 - %9.2f width 9, accuracy 2 - %9.2f width 9, accuracy 2 - %9.2f width 9, accuracy 0Copy the code
-
Other flag
- '+' is always a plus or minus sign of the output value; For %q (%+q) output is all ASCII characters (by escaping); Log values, positive numbers are preceded by a space and negative numbers by a negative sign; Using %x or %x for strings (%x or %x) adds a space between the printed bytes - '-' padding the right side of the output instead of the default left side (that is, switching from the default right to left); - '#' prefixes 0 (%#o) for octal numbers, 0x (%#x) or 0x (%#x) for hexadecimal numbers, removes the preceding 0x (%#p) pair %q (%#q) for %U (%#U), prints a space and a single quoted go literal; - '0' is padded with zeros instead of Spaces, followed by signs for numeric types;Copy the code
2.5 bufio
- The Bufio package implements buffered I/O. It wraps one IO.Reader or IO.Writer interface object and creates another object that also implements the interface and also provides buffering and some text I/O helper functions.
2.6 bytes
- The bytes package implements common functions that operate on []byte.
2.7 compress series
- A series of things related to compression
2.8 the crypto series
- Some column encryption and decryption related content
2.9 encoding series
- Encoding package defines an interface used by other packages to convert data between byte level and text representation
2.10 flag
- The flag package implements parsing of command-line arguments
2.11 the IO
- IO packages provide a basic interface to I/O primitives. The basic task of this package is to wrap existing implementations of these primitives (such as those in OS packages) into shared public interfaces that abstract out generic functions and attach some operations to the associated primitives.
2.12 the log
- The log package implements simple logging services.
2.13 math
- The Math package provides basic mathematical constants and mathematical functions.
2.14 net series
- Provides content related to network communication
2.15 OS
- The OS package provides platform-independent interfaces for operating system functions, such as file operations.
2.16 reflect
- The Reflect package implements runtime reflection, allowing programs to manipulate objects of any type.
2.17 strings
- The Strings package implements simple functions for manipulating characters.
2.18 other
There are too many libraries, please refer to doc. Golang. LTD /
reference
Go 英 文 版 [GO]
1. If you think this article is good, share and like it so that more people can see it
2. Welcome to pay attention to the front end line and surface of the public account and open the road of programming redemption.