Hi, I’m Mingo.
In their learning Golang this time, I wrote a detailed study notes on my personal number WeChat public Go programming time, for the language, I was a beginner, so writing should fit in with the new to classmates, if you are just learning the language, don’t focus on prevention, study together, Grow together.
My Github:github.com/iswbm/GolangCodingTime my online blog: golang.iswbm.com
When it comes to Go, the first impression that many people who have not touched it will be that it is very convenient for developers to write high performance and easy to understand programs quickly.
In Python (for example, Py, which I’m familiar with, and other major programming languages are familiar with), the barrier to concurrent programming is not low. You need to learn multi-process, multi-threading, asyncio, AIoHTTP, etc. You also need to understand the differences and advantages and disadvantages between them. Know how to choose different concurrency modes for different scenarios.
Golang, as a modern programming language, doesn’t require you to confront these complex problems. In Golang, you don’t need to learn how to create a process/thread pool, or know when to use multiple threads and when to use multiple processes. Because you don’t have to, and you don’t have to, the Goroutine (or coroutine) it provides natively is good enough to automatically take care of everything for you, and all you have to do is implement it, that’s all.
A Goroutine is a function in itself. When you call it directly, it is a normal function. If you prefix the call with the keyword go, you have opened a Goroutine.
// Start a coroutine and run the function go func()Copy the code
1. Preliminary use of coroutines
The entry point to a Go program is usually the main function, which runs first when the program starts. We call this the Main goroutine.
The go + func() method is used to start the coroutine only in main or code called below it.
Main is the main thread. When main completes, the thread terminates, and all running coroutines exit, regardless of whether the code is still running.
So when this code runs, it just prints hello, world, not hello, go. (Because it takes time to create a coroutine, and when hello, world is printed, it doesn’t have time to execute it.)
Import "FMT" func mytest() {fmt.println ("hello, go")} func main() {// Start a coroutine. world") }Copy the code
For cooutines just learning Go, you can block main using time.sleep to give other coroutines a chance to run completely, but you should note that this is not the recommended way (we’ll learn more about more elegant ways later).
When I add a line of time.sleep output to the code, it works as expected.
import (
"fmt"
"time"
)
func mytest() {
fmt.Println("hello, go")
}
func main() {
go mytest()
fmt.Println("hello, world")
time.Sleep(time.Second)
}Copy the code
The output is as follows
hello, world
hello, goCopy the code
2. Effects of multiple coroutines
To show you how concurrency works, here’s the simplest example
import ( "fmt" "time" ) func mygo(name string) { for i := 0; i < 10; I++ {FMT.Printf("In goroutine %s\n", name) Sleep(10 * time.sleep (10 * time.millisecond)}} func main() {go mygo(" coroutine 1 ") // time.Sleep(time.Second) }Copy the code
The output is as follows, observing that two coroutines execute concurrently, as if they were two threads
So we're going to have to figure out if we're going to have to figure out if we're going to have to figure out if we're going to have to figure out if we're going to have to figure out if we're going to have to figure out what the coroutine is So the coroutine number one is going to be one of the coroutines So we're going to have a coroutine number two, and we're going to have a coroutine number two Coroutines no. 2Copy the code
Through the simple example above, is not convinced by Go’s powerful concurrency features, synchronous code to asynchronous code, really only a keyword, do not need to use other libraries, simple and convenient.
This article only introduces the simple use of coroutine, the real concurrent program or combined with the channel to achieve. The content of channels will be covered in the next article.
A series of reading
01. Setup of development environment (Goland & VS Code)
02. Learn five ways to create variables
03. Data type: **Integer and floating point **
Data types: byte, RUNe, and String
05. Data types: Array and slice
06. Data types: dictionary and Boolean
07. Data type: pointer
08. Object-oriented Programming: Structures and Inheritance
09. An article to understand the function in Go
10. Go language flow control: if-else conditional statements
11. Go language flow control: switch-case selection statement
12. Go language flow control: for loop statement
13. Go language flow control: GOto unconditional jump
14. Go language flow control: defer the call
15. Object-oriented programming: Interfaces and polymorphism
Key words: make and new difference?
17. An article to understand the blocks and scopes in Go
18. Learn Go coroutine: Goroutine
19. Learn Go coroutines: Detail channels/channels
20. Several classic error cases of channel deadlock
21. Learn the Go coroutine: WaitGroup
22. Learn Go coroutines: mutex and read-write locks
23. Exception handling in Go: Panic and recover
24. Super detailed interpretation of Go Modules past life and the introduction to use
25. Go language about package import must learn 8 knowledge points
26. How to open source your modules for others to use?
27. What about type assertions in Go?
28. These five points will help you understand the select usage of Go