How do I transition from Java to Go engineer block chain

How do I transition from Java to Go engineer block chain

This article comes from developing a statement of core than the original chain

Foreword

I join in than before the original chain has been doing Java development, and when I first saw Go a little hesitant, afraid not pass the interview, the result of considering whether to master a language does not so high, I smooth entry than the original chain, and Go within two weeks and be able to quickly grasp the core of the development project. Java language has a mature project on a larger advantage, but it does have a lot of weaknesses in the block chain development, such as on the co-processing process, as well as the Java language itself is not flexible enough, and so, of course, choose to use mainly because of its Go prevalence in the block chain.

Block chain of popular languages

In the development of public circle block chain chain, we find a number of popular programming language, C ++, Golang, Python and recently played Rust and so on.

A little more well-known programming language to be used in the project statistics, as shown below:

The older generation of male chain, such as Bitcoin, Litcoin general use of C / C ++ more (we look at that time, then Go not up yet), a new generation of public chains such as Ethernet Square, the Super League leaders chains books, began more using the Go language, of course, we see Rust momentum is very fierce, many public chain the past two years such as Boca, Grin Rust are beginning to use language developed.

Advantages of the Go language

Simple deployment

Go is a static compiled executable file, in addition to other glibc no external dependencies. This makes deployment became very easy: on the target machine requires only a basic system and the necessary management and monitoring tools, totally do not need to worry about a variety of packages required for the application, library dependencies, greatly reducing the maintenance burden. Can be directly compiled into machine code, does not depend on other libraries, glibc version has certain requirements, the deployment is to throw up a file is complete.

Outstanding Performance

Although not as good as C and Java, but typically an order of magnitude native Python application or higher than, for writing some bottlenecks in business. Memory footprint is also very province.

Concurrency & channel

Goroutine and channel makes writing highly concurrent server software considerably easier, in many cases absolutely no need to consider locking mechanisms and the problems it poses. Go single application can effectively utilize a plurality of CPU cores, and good performance in parallel.

Good design language

Go 非常简单,且易于学习。从学术的角度讲 Go 语言其实非常平庸,不支持许多高级的语言特性;但从工程的角度讲,Go 的设计是非常优秀的:规范足够简单灵活。正是由于 Go 的简单性,任何的 Python、Elixir、C++、Scala 或者 Java 开发者皆可在一月内组建成一个高效的 Go 团队。

标准库&工具

Go目前已经内置了大量的库,特别是网络库非常强大。更重要的是 Go 自带完善的工具链,大大提高了团队协作的一致性。比如 gofmt 自动排版 Go 代码,很大程度上杜绝了不同人写的代码排版风格不一致的问题。把编辑器配置成在编辑存档的时候自动运行 gofmt,这样在编写代码的时候可以随意摆放位置,存档的时候自动变成正确排版的代码。此外还有 gofix, govet 等非常有用的工具。

团队牛逼

Go语言后面的支持者是Google,语言足够在各种场景下得到检验,同时创始人还是C语言之父,对后续的发展和创新可期。

Go成功的项目

Go语言在云时代得到了比较广泛的应用,特别是Docker和K8s这样的杀手级产品的出现让Go语言在工程界占有一席之地 除此之外Go语言还有非常多的成功运行中的软件: nsq:bitly开源的消息队列系统,性能非常高,目前他们每天处理数十亿条的消息 packer:用来生成不同平台的镜像文件,例如VM、vbox、AWS等,作者是vagrant的作者 skynet:分布式调度框架 Doozer:分布式同步工具,类似ZooKeeper Heka:mazila开源的日志处理系统 cbfs:couchbase开源的分布式文件系统 tsuru:开源的PAAS平台,和SAE实现的功能一模一样 groupcache:memcahe作者写的用于Google下载系统的缓存系统 god:类似redis的缓存系统,但是支持分布式和扩展性 gor:网络流量抓包和重放工具

生态卡位和隐性标准

除了打铁还需自身硬之外,还有些机遇和运势,让区块链选择的了Go语言。我们来看区块链2.0以来最成功的公链和联盟链代表,以太坊和超级账本Fabric,无一例外都选择使用Go作为开发语言(虽然以太坊其实也有其他语言的客户端版本,但进入到Homestead阶段以后,Go客户端占据了主导地位),这两大超级区块链的影响力可不是一般项目可以比拟的,不仅在生态中占据了大的坑位,事实上还隐性的制定了区块链的标准,不论是公链中的智能合约,还是联盟链的技术,都绕不开以太坊和Fabric,那么对于一家想要做区块链技术选型的公司来说,最快捷的实现是什么?自然是直接照搬这两个项目的创新,再捷径一点就是直接拿开源代码改,那么自然Go语言就成为后来者的首选,换种语言重新实现一遍难度也不小,而且如果选择一些创新但不是非常成熟的语言还会缺失一些特定库的支持从而导致项目无法开展。

很多人对以太坊的影响力毋庸置疑,但实际上Fabric在企业区块链部署上的影响力更不容小觑:

图表来源《2019年全球企业区块链基准研究报告》

Hyperledger Fabric是已部署的企业区块链网络中使用最多的协议框架,超级账本Hyperledger(其中Fabric作为旗舰协议)是集成商和软件开发平台最常支持的协议框架,比例达到了53%。 而在所有的区块链技术书籍里面,有关超级账本的书籍是卖的最为火爆这个事实也是侧面印证了超级账本Hyperledger的影响力。

比原链在Go语言中的实践

在选型编程语言的过程中,考量了C,C++, Java,但C/C++大项目维护难度大,而Java又略显笨重,此时Go语言已经在区块链项目上大放异彩,也逐渐形成技术和人才的一个头部效应,那么顺应潮流进行技术选型自然也会减少初始比原链项目遇到的阻力,当然在逐渐开发过程中,我们也感受到了选用Go语言带来的便利和优势。

Go在区块链上的一个Case

从技术上来说,区块链节点是需要多模块异步协同工作的,所以Go语言并发性和通道就显得非常有优势,我们看下面交易验证的例子:

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
}


我们使用Routine+Ch+WaitGroup在30行代码之内,就可以构建一个并发的验证交易的功能,在高配置的服务器的情况下,可以跑出10万以上的TPS。

轻松变成Go语言大师

我当初进入比原之前也没有做过Go语言开发,但都能够很快上手,基本在半个月内能够参与核心代码的开发和维护了(对于从C/C++/Java有经验的开发者尤其快速),这就是语言简单对团队构建带来的好处。

统一的协作

从协作上来说,通过gofmt 自动排版 Go 代码,能够让核心团队成员甚至社区开发者提交的代码风格的差异性降到最小,提升项目的整体质量和可维护性。

小结

Go语言本身的特质和优势为其做好了铺垫,而以太坊和超级账本两个超级区块链项目的加持也让Go语言成为了很多区块链项目的首选,比原链选用Go语言也充分体会到了其开发区块链底层的优势,但是无需落入语言之争的陷阱,讲求实用主义才是做工程应有之义,比原链核心项目是用Go语言完成,但是周边的很多子项目也有用Java,Python或者JavaScript实现,毕竟生态的多样性才是一个项目长久的根本。

Guess you like

Origin www.cnblogs.com/bytom/p/12132862.html