coroutines

Coroutines are one of golang’s most important features. Before coroutines, threads were used as the smallest unit of scheduling. Coroutines can be understood as user-mode, logical level threads. With coroutines, we can easily achieve high concurrency. Let’s say you’re going to do three things, let’s say you’re going to execute a, B, and C. How do I write the code? The way we normally write it is

a()
b()
c()
Copy the code

You can’t do B until A is done, and you can’t do C until B is done. But with coroutines you can

go a()
go b()
go c()
Copy the code

Three at the same time, taking full advantage of multi-core performance.

The pipe

Pipeline concept

  1. Similar to pipes in Unix
  2. First in first out
  3. Thread-safe, multiple Goroutine access at the same time, no lock required
  4. A channel is typed, and an integer channel can only hold integers

Definition of pipeline

var ch0 chan int
var ch1 chan string
var ch2 chan map[string]string

type stu struct{}

var ch3 chan stu
var ch4 chan *stu
Copy the code

Pipes are used to pass data between coroutines. It is generally divided into buffered pipelines, and no buffered pipelines. What if there is data communication between two coroutines, then you can pipe it. Golang is designed to replace shared memory with communication.

func a() {
    ch <- 1
}

func b() {
    ch <- 
}
Copy the code

The pipe can be buffered, which means that multiple pieces of data can be put into the pipe until it fills up.

C1 :=make(chan int) null c2:=make(chan int,1) nullCopy the code

C1 <-1 no buffering: not only does it put 1 into c1, but it does not continue until another ctrip <-c1 takes over the parameter, otherwise it is blocked.

Buffered: c2<-1 does not block, because the buffer size is 1, and only blocks if the second value is placed before the first value is taken away.

This is a common interview question. It’s really the difference between synchronous and asynchronous.

Interface interface

An interface is a collection of method signatures that An Object can implement An interface is a collection of function signatures. Concrete structures can implement them. Such as:

type Shape interface {
    Area() float64
    Perimeter() float64}Copy the code

Perimeter this is an interface that declares two methods Area and Perimeter where we can define specific structures to implement this interface.

// type rect
type Rect struct {
    height float64
    weight float64
}

func (p *Rect) Area() float64 {
    return p.height * p.weight
}

func (p *Rect) Perimeter() float64 {
    return 2 * (p.height + p.weight)
}
Copy the code

Interfaces can provide object-oriented programming capabilities that, once an interface is implemented, can be used to point to objects of a specific type. A reference of the same type to different objects is polymorphic. As you can see, the derivation in golang, unlike c++ and Java, is not explicitly specified, but simply implements all the methods defined by the interface. Therefore, Golang’s inheritance relationship is non-invasive, which is also golang’s feature and advantage.