Go开发关键技术指南:Overview

2020-01-10  本文已影响0人  winlinvip

Overview

本文讨论了服务器领域常见的并发等问题,也涉及到了工程化相关的问题,也整理了C背景程序员对于Go的GC以及性能的疑问,探讨了Go的错误处理和类型系统最佳实践,以及依赖管理的难处,接口设计的正交性,当然也包含我们在服务器开发中对于Go实践的总结,有时候也对一些有趣的问题做深度的挖掘,也列出了Go重要的事件和资料集合,还有Go2的进展和思考。我更想从问题本身出发,不局限于Go语言,探讨服务器中常常遇到的问题,最后回到Go如何解决这些问题,提供Go开发的关键技术指南。

About the Name

The Go Programming Language到底是该叫GO还是GOLANG?Google搜Why Go is called Golang能搜到几篇经典帖子。

Rob Pike在Twitter上特意说明是Go,可以看这个The language is called Go:

Neither. The language is called Go, not Golang. http://golang.org is just the the web site address, not the name of the language.

那么在另外一个地方也说明了也是Go,可以看这个The name of our language is go

The name of our language is Go
Ruby is called Ruby, not Rubylang.
Python is called Python, not Pythonlang.
C is called C, not Clang. No. Wait. That was a bad example.
Go is called Go, not Golang.

Yes, yes, I know all about the searching and meta tags. Sure, whatever,
but that doesn't change the fact that the name of the language is Go.

Thank you for your consideration.

这里举了各种例子说明为何不加lang的后缀,当然有个典型的语言是加的,就是Erlang。于是就有回复说“Erlang Erlang, Let's just call it Er.”

那么为何很多时候Go和Golang都很常用呢?在Why is the Go programming language usually called Golang中说的比较清楚:

It’s because “go domain” has been registered by Walt Disney and so Go creators couldn’t use it.
So, they have decided to use golang for the domain name. Then the rest came.

Also, it’s harder to search things on search engines just using the word Go. Although, Rob Pike is
against this idea but I disagree. Most of the time, for the correct results you need to search for
golang.

It’s just Go, not golang but it sticked to it.

讲个笑话先,用百度搜下为何Go叫做Golang,一大片都是类似本文的鸡汤煲,告诉你为何Go才是天地间最合适你的语言,当然本文要成为鸡汤煲中的战斗煲,告诉你全家都应该选择Go语言。

为何Go语言名字是Go,但是经常说成是Golang呢?有以下理由:

  1. go.org被注册了,正在卖,也不贵才1698万。所以Go只能用golang.org
  2. 搜点啥信息如果搜go,太宽泛了,特别是go还没有这么多用户时,搜golang能更精确的找到答案。

为什么在名字上要这么纠结呢?嗯嗯,不纠结,让我们开始干鸡汤吧。

Why Go?

考虑一个商用的快速发展的业务后端服务器,最重要的是什么?当然是稳定性了,如果崩溃可能会造成用户服务中断,崩溃的问题在C/C++服务器中几乎是必然的:

  1. 稳定是一种假象。想象一个C服务器,一般不会重头码所有的代码,会从一个开源版本开始,或者从一些网络和线程库开始,然后不断改进和完善,由于业务前期并不复杂,上线也没有发现问题,这时候可以说C服务器是稳定的吗?当然不是,只是Bug没有触发而已,所有崩溃的Bug都几乎不是本次发布导致的问题。野指针和越界是C服务器中最难搞定的狼人,这些狼人还喜欢玩潜伏。
  2. 稳定是短暂,不稳定是必然和长期。一般业务会突飞猛进,特别是越偏上层的业务,需要后端处理的逻辑就越多,至于UTest和测试一般只存在于传说中,随着业务的发展,潜伏的狼人越来越多,甚至开源的库和服务器中的狼人也开始出来作妖。夜路走多了,总会碰到鬼。碰到鬼了怎么办,遇鬼杀鬼了,还能被它吓尿不成,所以就反思解决Bug,费了老劲了,又白了几根头发,终于迎来短暂安宁,然后继续写Bug。
  3. 最普遍的问题还是内存问题导致崩溃,一般就是野指针和越界。空指针问题相对很容易查,除零之类的典型错误也容易处理。最完善的解决办法,就是实现GC,让指针总是有效,无效后再释放,越界时能检测到这样容易解决问题;嗯,其实Go早期的版本就和这个很类似了,要实现带GC的C的同学,可以参考下Go的实现。
  4. 线上的CPU和内存的问题,一般不方便使用工具查看,而线上的问题有时候很难在本地重现。如何能直接获取线上的Profile数据,需要程序本身支持。比如提供HTTP API能获取到Profile数据?关键是如何采集这些数据。

Go的使命愿景和价值观:

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

Go is a concurrent open source programming language developed at Google. Combines native compilation and static types with a lightweight dynamic feel. Fast, fun, and productive.

Go is an attempt to make programmers more productive. The first goal is to make a better language to meet the challenges of scalable concurrency. The larger goal is to make a better environment to meet the challenges of scalable software development, software worked on and used by many people, with limited coordination between them, and maintained for years.

Go语言的关键字:

参考The Path to Go1: What is Go?Another Go at Language Design

参考Go: a simple programming environment

Go是面向软件工程的语言,Go在工程上的思考可以读Go at Google: Language Design in the Service of Software EngineeringLess is exponentially more。Go最初是解决Google遇到的大规模系统和计算的问题,这些问题如今被称为云计算,参考Go, Open Source, Community

GITHUT上显示Go的项目和PR一直在上升,如下图所示。

image.png

2014云计算行业中使用Go的有:Docker, Kubernetes, Packer, Serf, InfluxDB, Cloud Foundry’s gorouter and CLI, CoreOS’s etcd and fleet, Vitess, YouTube’s tooling for MySQL scaling, Canonical’s Juju (rewritten in Go), Mozilla’s Heka, A Go interface to OpenStack Swift, Heroku’s Force.com and hk CLIs, Apcera’s NATS and gnatsd。

2018年全球使用Go的公司数目有:US(329), Japan(79), Brazil(52), India(49), Indonesia(45), China(32), UK(32), Germany(28), Israel(24), France(17), Netherlands(16), Canada (15), Thailand(14), Turkey(14), Spain(12), Poland(11), Australia(9), Russia(9), Iran(8), Sweden(7), Korea(South)(6), Switzerland(6), Ukraine(5)。

参考Go as the emerging language of cloud infrastructure,以及The RedMonk Programming Language Rankings: June 2018,还有GoUsers,以及Success Stories

参考"Go: 90% Perfect, 100% of the time" -bradfitz, 2014

参考Nine years of Go: Go Contributors,社区贡献的代码比例。

我们一起看看这些Go牛逼的特性,详细分析每个点,虽然不能涵盖所有的点,对于常用的Go的特性我们做一次探讨和分析。

Milestones

看看Go做到了哪些,Go的重要事件:

Links

由于简书限制了文章字数,只好分成不同章节:

上一篇 下一篇

猜你喜欢

热点阅读