Why do we use GO language for blockchain?

In the development circle of the blockchain public chain, we have found some popular programming languages, including C ++, Golang, Python, and the recent new Rust.

Let's make a statistics of the programming languages ​​used by more famous projects, as shown below:

333

The old generation of public chains, such as Bitcoin, Litcoin generally uses C / C ++ more (we look at that time, when Go was not up at the time), the new generation of public chains such as Ethereum, the league's leading super ledger, started more Using Go language, of course, we see that the development momentum of Rust is also very fierce. In the past two years, many public chains such as Boca, Grin have started to develop in Rust language.

Advantages of Go

Simple deployment

Go compiles a static executable file with no external dependencies other than glibc. This makes deployment extremely convenient: only a basic system and necessary management and monitoring tools are required on the target machine, and there is no need to worry about the dependencies of various packages and libraries required by the application, which greatly reduces the burden of maintenance. It can be directly compiled into machine code, and does not depend on other libraries. The version of glibc has certain requirements, and the deployment is completed by throwing a file.

Excellent performance

Although not as good as C and Java, it is usually an order of magnitude higher than native Python applications and suitable for writing some bottlenecks. The memory footprint is also very low.

Concurrency & Channel

Goroutine and channel make it easy to write highly concurrent server software. In many cases, there is no need to consider the locking mechanism and the various problems caused by it. A single Go application can also effectively use multiple CPU cores, and the performance of parallel execution is good.

Good language design

Go is simple and easy to learn. Academically speaking, the Go language is actually very mediocre and does not support many advanced language features; but from an engineering perspective, Go's design is very good: the specification is simple and flexible enough. Because of the simplicity of Go, any Python, Elixir, C ++, Scala or Java developer can form an efficient Go team in one month.

Standard Library & Tools

Go has a lot of built-in libraries, especially the network library is very powerful. More importantly, Go comes with a comprehensive tool chain, which greatly improves the consistency of team collaboration. For example, gofmt automatically typesets Go code, which largely eliminates the problem of inconsistent style of code written by different people. Configure the editor to automatically run gofmt when editing the archive, so that you can place it at will when you write the code, and automatically become the correctly typed code when you archive. There are also very useful tools such as gofix, govet.

Team Niubi

The backer of the Go language is Google. The language is enough to be tested in various scenarios. At the same time, the founder is the father of the C language, and he can expect future development and innovation.

Go successful projects

The Go language has been widely used in the cloud era, especially the emergence of killer products such as Docker and K8s has made Go a place in the engineering world. In addition, Go has a lot of successful software running:

nsq: bitly open source message queue system with very high performance. Currently they process billions of messages every day.

packer: used to generate image files for different platforms, such as VM, vbox, AWS, etc., the author is the author of vagrant

skynet: distributed scheduling framework Doozer: distributed synchronization tool, similar to ZooKeeper

Heka: mazila open source log processing system

cbfs: couchbase open source distributed file system

tsuru: The open source PAAS platform has exactly the same functions as SAE.

groupcache: cache system for Google download system written by memcahe author

god: cache system similar to redis, but supports distributed and extensible

gor: network traffic capture and replay tool

Eco-bit and recessive standards

In addition to iron, you need to be hard, and there are some opportunities and fortunes that have made the blockchain choose the Go language. Let's look at the most successful representatives of the public and alliance chains since Blockchain 2.0. Ethereum and the Hyperledger Fabric have all chosen to use Go as the development language. (Although Ethereum also has client versions in other languages, but enter After the Homestead stage, the Go client has occupied a dominant position.) The influence of these two super-blockchains is not comparable to that of ordinary projects. It not only occupies a large pit in the ecology, but it is also implicitly formulated. With the standards of the blockchain, both the smart contracts in the public chain and the technology of the alliance chain cannot bypass Ethereum and Fabric, so for a company that wants to do blockchain technology selection, it is the fastest What is the implementation? It is natural to directly copy the innovations of these two projects, and a shortcut is to directly change the open source code, so naturally the Go language has become the first choice of latecomers. It is not difficult to re-implement it in another language. If you choose some innovation but not Very mature languages ​​will also lack support for specific libraries, making projects impossible.

Many people's influence on Ethereum is undoubted, but in fact, Fabric's influence on enterprise blockchain deployment is even more important:

1577777160836-7a4ea9e8-46f9-448c-85e2-379e64001b3c

Chart source “ 2019 Global Enterprise Blockchain Benchmark Research Report ''

Hyperledger Fabric is the most used protocol framework in deployed enterprise blockchain networks. Hyperledger (with Fabric as the flagship protocol) is the most commonly supported protocol framework for integrators and software development platforms, with a ratio of 53%. The fact that books on hyperledgers are the most popular among all blockchain technology books also confirms the influence of Hyperledger.

The practice of bis original chain in Go language

In the process of selecting a programming language, we considered C, C ++, and Java, but large C / C ++ projects are difficult to maintain, and Java is a little clunky. At this time, Go has already shined on blockchain projects. It also gradually formed a head effect of technology and talents, so technology selection in accordance with the trend will naturally reduce the initial resistance to the original chain project. Of course, during the gradual development process, we also felt that the choice of Go language brought the Convenience and advantages.

Go's Case on the blockchain

Technically, blockchain nodes need multiple modules to work together asynchronously, so Go's concurrency and channels appear to be very advantageous. Let's look at the following example of transaction verification:

func ValidateTxs(txs []*bc.Tx, block *bc.Block) []*ValidateTxResult { txSize := len(txs) //init the goroutine validate worker var wg sync.WaitGroup workCh := make(chan *validateTxWork, txSize) resultCh := make(chan *ValidateTxResult, txSize) closeCh := make(chan struct{}) for i := 0; i <= validateWorkerNum && i < txSize; i++ { wg.Add(1) go validateTxWorker(workCh, resultCh, closeCh, &wg) } ​ //sent the works for i, tx := range txs { workCh <- &validateTxWork{i: i, tx: tx, block: block} } ​ //collect validate results results := make([]*ValidateTxResult, txSize) for i := 0; i < txSize; i++ { result := <-resultCh results[result.i] = result } ​ close(closeCh) wg.Wait() close(workCh) close(resultCh) return results } 

We use Routine + Ch + WaitGroup within 30 lines of code to build a concurrent transaction verification function. With a highly configured server, we can run more than 100,000 TPS.

Easily become a Go master

In terms of talents, some members of the original chain core development team have not done Go language development before, but they have been able to get started quickly, and can basically participate in the development and maintenance of core code within half a month. C ++ / Java experienced developers are especially relaxed), this is the benefit of simple language for team building.

Unified collaboration

In terms of collaboration, gofmt automatically typesets Go code to minimize the differences in code styles submitted by core team members and even community developers, and improve the overall quality and maintainability of the project.

summary

The characteristics and advantages of Go itself pave the way, and the blessing of the two super blockchain projects, Ethereum and Hyperledger, has made Go the first choice for many blockchain projects. I fully realized the advantages of developing the bottom layer of the blockchain, but there is no need to fall into the trap of language disputes. Pragmatism is the meaning of engineering. The core project of the original chain is completed in Go language, but many surrounding The project can also be implemented in Java, Python or JavaScript. After all, the diversity of the ecology is the fundamental foundation of a project for a long time.