Error message here!

Hide Error message here!


Error message here!


Hide Error message here!


Error message here!



01. One of the design philosophy of go language: simplicity

Brother Huanxi 2021-01-21 16:03:09 阅读数:13 评论数:0 点赞数:0 收藏数:0

Video address of this article

Go It has been more than ten years since the birth of language ,Go The charm of language makes it have millions of support users all over the world . What makes a lot of developers learn Go Or from other languages to Go Language ? The root is Go The design philosophy of language .
About Go The design philosophy of language ,Go The fathers of language and Go The developers of the core team didn't give a clear official statement . But here I will be based on my personal understanding of them as well as Go Community mainstream views and code behavior 、 Analyze and summarize , List three Go The design philosophy of language . Understanding these design philosophies will help readers form Go Native programming thinking 、 Write high quality Go Code has a positive impact .

The first principle : Simple , Less is more

Usually when we ask Go Language lover They asked such a question :“ Why do you like Go Language ” after , We're going to get a lot of answers , Such as :

  • Compilation speed is fast
  • Fast execution
  • Single binary , Simple deployment
  • Great toolset
  • The standard library is super powerful
  • Built in concurrency
  • interface great
  • Cross platform Easy
  • … …

The top and most popular answer is “Go It's simple ”, It's also related to the official Go The results of the language survey are consistent .

And other languages like C++、Java Such as compared to , By constantly adding new features to attract programmers to the mainstream programming language ,Go At the very beginning of language design, designers in the world chose to refuse to take the road of integrating language features , I chose “ subtracting ”, I chose “ Simple ”, Let users use enough -- Simple , They leave complexity to the design and implementation of the language itself , Left to Go The core development team itself , And it will be simple 、 Ease of use and clarity are left to the vast majority of Go Language users . therefore , What we have before us today is this Go Language :

  • concise -- It only has 25 Key words
  • Built in garbage collection , Reduce the burden of memory management for developers
  • No header file
  • Explicitly depend on (package)
  • There is no circular dependence (package)
  • Constants are just numbers
  • Case determines visibility
  • Any type can have methods ( There is no class )
  • No subtype inheritance ( No subclasses )
  • The interface is implicit ( There is no need to “implements” Statement )
  • The method is the function
  • An interface is just a collection of methods ( No data )
  • Methods match by name only ( Not by type )
  • There is no constructor or destructor
  • Assignment is not an expression
  • The order of evaluation defined in assignments and function calls ( nothing “ Sequence point ” Concept )
  • Memory is always initialized to zero
  • No, const Or other types of annotation Syntax
  • No template / Generic
  • No abnormal (exception)
  • Built in strings 、 section (sl Dian (map) type
  • Built in array boundary checking
  • Built in concurrency support
  • … …

There is a compromise between the pros and cons of any design . We see Go The designer chose “ Simple ” This is reflected in the removal or optimization of the grammatical elements and language mechanisms that have been proved to be poor or difficult to control by developers in the past , And put forward some of their own innovative design ( such as : The case of the header determines the visibility 、 Initial zero value of memory allocation 、 Built in to go Keyword implementation of concurrent support ).

Go Designers use “ The minimum principle ” The way , That is, there is only one way or as few ways as possible to accomplish a thing , This greatly reduces the mental burden of developers in choosing the path and understanding the path chosen by others .

Rob Pike It was said that :“Go Language is actually complex , But it just makes people feel simple ”. The deep meaning behind this sentence is “ Simple ” Behind the choice is Go The complexity of language's realization , And these complexities are Go The designer of language “ hide ” up , For example, we use a simple keyword “go” You can take care of concurrency , Behind this simplicity is actually Go The result of the team's careful design and continuous effort .

Besides ,Go My simple philosophy is also reflected in Go1 Compatibility 2 The proposed . For developers facing engineering problem solving ,Go1 It greatly reduces the consumption of language version upgrade on the engineering level , Give Way Go The engineering practice of this project has become extremely simple .
Go1 Excerpts from compatibility notes

Go1 Two things are defined :
First of all , The norms of language ;
second , A set of core API The specification of , namely Go Standard library “ Standard library ”.

API It could grow , Add new packages and features , But not to destroy the existing Go1 The way code works .
from Go 1.0 Released so far ,Go1 Compatibility is always well adhered to , In the beginning Go 1.4 The code you write can now go through the latest Go 1.15 Version of the compilation and normal operation .
Go This innovation of language “ Simple ” Design is not understood by programmers in the first place , But in real use Go after , This kind of design philosophy level “ Simple ” It extends to Go All aspects of language programming applications , It continues to affect Go The formation of language programming thinking .


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