Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

02. Go language design philosophy II: combination

Brother Huanxi 2021-01-21 16:02:58 阅读数:14 评论数:0 点赞数:0 收藏数:0

Video address of this article

Java 、C++ And so on ( hereinafter referred to as OO) Language through complex and huge 、 Top down typology 、 Inherit 、 Explicit interface implementation and other mechanisms couple the various parts of the program , But in Go We can't find classics in language OO The grammatical elements of 、 Type systems and inheritance mechanisms , Or say Go Language does not belong to classics in essence OO Language category . In this case , A lot of people will ask : that Go How does language couple all parts of a program ?Go The design philosophy that language follows is also a combination .

Before introducing the combination , We can learn about Go How to lay a solid foundation for the application of combinatorial philosophy when designing grammatical elements .

stay Go Language design level ,Go The designer is gopher We provide syntax elements for subsequent combinations , Include :

  • Go Language has no type system (type hierarchy), Types are independent , There is no concept of subtypes ;
  • Each type can have its own set of methods , Type definitions and method implementations are orthogonal independent ;
  • Interface (interface) Between its implementation " Implicit Association ";
  • package (package) They are relatively independent , There is no concept of subpackages .

We see both bags 、 Interfaces are also specific type definitions ( A collection of methods including types ),Go Language presents us such a picture : Building blocks that are not related , But every building block is wonderful . Now the job in front of us is to establish a connection between these building blocks in the most appropriate way ( coupling ), To form a " whole ".Go It's a combination , And the only way .
Go The most intuitive combination of grammatical elements provided by a language is type embedding, That is, type embedding . Embed... By type , We can embed the implemented functions into the new types , To quickly meet new types of functional requirements , It's kind of like a classic OO Of “ Inherit ”, But in principle it's the same as the classic OO It's totally different . This is a kind of Go Well designed “ Grammatical sugar ”, There is no relationship between the embedded type and the new type , They don't even know each other at all , There is no classic OO What kind of parent 、 The relationship between subclasses and up 、 Move down (type casting). When a method is called through a new type instance , The matching of methods depends on the method name , Not the type . This combination , I call it “ Vertical combination ”, That is, by type embedding , Quickly make a new type “ Reuse ” Other types of capabilities that have been implemented , Realize the vertical expansion of functions .
Here's an example of type embedding :

Go Standard library :sync/pool.go

 type poolLocal struct {
...
Mutex // Protects shared.
...
}

We are poolLocal This struct Embedded types in Mutex, Embedded Mutex The method collection of type is promoted to the outer type . such as , there poolLocal Will have Mutex Type of Lock and Unlock Method . When actually called , Method calls are actually passed to poolLocal Medium Mutex example .
We often see something like this in the standard library interface Type embedded code :

 type ReadWriter interface {
Reader
Writer
}

By means of interface Embedded in interface type, Implement aggregation of interface behavior , Make up the big interface , This approach is common in standard libraries , Has become Go A common use of language .

interface yes Go The real magic of language , yes Go An innovative design of language , It's just a collection of methods , And the relationship between it and the implementer is implicit , It minimizes the coupling between parts of the program , At the same time, it is also a connection between the various parts of the program “ link ”. implicit interface The realization will be satisfied inadvertently : Dependency abstraction 、 Richter replacement 、 Interface isolation, etc , This is very necessary in other languages " deliberately " It can only be realized through the design and planning of the company , But in Go interface Look at , It's all natural .

adopt interface A way to combine the various parts of a program , I call it horizontal combination here . Horizontal combination of “ Pattern ” quite a lot , such as : A common way is : By accepting interface Type parameters are combined with ordinary functions , For example, the following code .

 func ReadAll(r io.Reader) ([]byte, error)
func Copy(dst Writer, src Reader) (written int64, err error)

ReadAll adopt io.Reader This interface will io.Reader Implementation and ReadAll The low coupling levels of the packages are grouped together . Similar horizontal combinations “ Pattern ” also wrapper、middleware etc. .

Besides ,Go The built-in concurrency of the language can also be realized by combination “ The cascade of computing power ”, such as : adopt goroutine+channel The combined implementation of is similar to Unix Pipe The ability of .

thus , The application of combinatorial principle shows that Go The skeleton of the program . Type embeddedness provides the type with the ability to extend vertically ,interface It's the key to horizontal combination , It's like... On the body of a program “ The joints ”, Give the connection “ The joints ” Each of the two parts of “ Free activities ” The ability of , And on the whole, it realizes some function . The combination also allows you to follow “ Simple ” principled Go The expressive power of language is no less than that of other complex mainstream programming languages .

image

Copyright statement
In this paper,the author:[Brother Huanxi],Reprint please bring the original link, thank you

编程之旅,人生之路,不止于编程,还有诗和远方。
阅代码原理,看框架知识,学企业实践;
赏诗词,读日记,踏人生之路,观世界之行;