Understand the direction

  • Some core concepts: concurrency or other (and implementation logic)
  • Go language features:
  • Packaging/environment/development dependency tools (get and build code)
  • Go language built-in types: array/slice/map
  • Built-in type system: structure type, named type, interface, type nesting
  • How does the scheduler/concurrency/channel work
  • Common concurrent mode, goroutine pool implementation, how to reuse resources
  • Standard library use: log, JSON, IO
  • Testing/benchmarking

Details of the features

Born for concurrency

  • Concurrency makes better use of multi-core cpus
  • Concurrency is based on a GoRoutine, similar to but not threads, and can be thought of as a virtual thread
  • The GO runtime dispatchers Gourders and properly allocates goroutine to each CPU to maximize CPU usage
  • Goroutines communicate with each other using channels
package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time") const (numberGoroutines = 4 // Number of goroutines to use taskLoad = 20 // number of jobs to process) var wg sync.waitgroup //init Initialization package Execute func before running any other codeinit(){rand.seed (time.now ().unix ())} // entry function funcmainWg.add (numberGoroutines) {make(chan string,taskLoad) // Start GoRoutine to handle tasks:forgr :=1; gr<=numberGoroutines ; Gr ++ {go worker(tasks,gr)} // Add a group of tasks to completeforpost :=10; post<taskLoad; post++ { tasks <- fmt.Sprintf("Task: %d\n"Wg.wait ()} // work function func worker(tasks chan string,worker int){ // Inform the function to defer wg.done ()for{
		task,ok := <-tasks;
		if! Ok {// indicates that the channel is empty and closed."Worker:%d: Shutting Down \n ",worker)

			return
		}

		fmt.Printf("Worker:%d: Started %s\n",worker,task) // wait for a time to start work again.Sleep := rt.int63n (100) time.sleep (time.duration (sleep)* time.millisecond) ftt.printf ()"Worker:%d:Completed %s\n",worker,task)
	}

}

Copy the code
  • The output
H -- -- -- -- -- E -- -- -- -- -- -- -- -- -- -- L L -- -- -- -- -- -- -- -- -- - aaaa: O 0 aaaa: 1 aaaa: 2 aaaa: 3 aaaa: 4 aaaa: 5 aaaa: six aaaa: 7 aaaa: 8 aaaa: 9 after the assignment: 100 --- 200Multiply 2 * 5 * 6 = 60 Process finished withexit code 0
Copy the code

.