为什么使用 Go 语言？
编程语言 Go
几个星期前，我一个朋友问我：“为什么要关心 Go 语言”？ 因为他们知道我热衷于 Go 语言，但他们想知道为什么我认为*其他人*也应该关心。本文包含三个我认为 Go 是重要的编程语言的原因。


![](/data/attachment/album/201704/02/180352wzvhlh955q5zl9ff.jpg)


### 安全


个人而言，你和我或许完全有能力在 C 中编写程序，既不会泄漏内存，也不会不安全地重复使用内存。然而，整体上，即使有超过 [40 年](https://en.wikipedia.org/wiki/C_(programming_language))的经验，用 C 的程序员也无法可靠地这样做。


尽管静态代码分析、valgrind、tsan 以及 “-Werror” 已经存在了几十年，却很少有证据表明这些工具被广泛认可，更不用说广泛采用。总而言之，事实表明，程序员根本无法安全地管理自己的内存。现在是离开 C 的时候了。


Go 不需要程序员直接管理内存，所有内存分配都由语言运行时自行管理，使用前初始化，必要时检查边界。它肯定不是提供这些安全保障的第一个主流语言，Java（1995）可能是该冠军的竞争者。关键是，世界对不安全的编程语言没有胃口，所以人们默认认为，Go 是内存安全的。


### 开发人员生产力


从 20 世纪 70 年代末，开发人员的时间变得比硬件所耗费的时间更昂贵了。开发人员的生产力是一个不断扩展的话题，但它归结为这一点：你花了多少时间做有用的工作，又有多少时间等待编译器或者失望地迷失在外部代码库中。


有个笑话说 Go 是在等待 [C ++ 程序编译](https://commandcenter.blogspot.com.au/2012/06/less-is-exponentially-more.html)时开发的。快速编译是 Go 的一个重要功能，也是吸引新开发人员的关键工具。虽然编译速度仍然是一个[永久的战场](https://dave.cheney.net/2016/11/19/go-1-8-toolchain-improvements)，但公平地说，在其他语言中需要几分钟的编译，在 Go 中只需要几秒钟。


Go 程序员意识到生产力的更根本的问题是代码是**为了读而写的**，所以将[代码的阅读行为放在编写之上](https://twitter.com/rob_pike/status/791326139012620288)。Go 通过工具和自定义来强制所有代码格式化成特定的样式。这消除了学习项目特定语言的方言时的困难，并有助于发现错误，因为它们**看上去**就是不正确。


由于专注于分析和机器辅助，Go 开发人员开始采用越来越多的工具来发现常见的编码错误，这种工具从来没有在 C 语言开发者中产生共鸣 - Go 开发人员**希望**工具帮助他们保持代码清洁。


### 并发性


十多年来，芯片设计师一直在警告[免费午餐将会结束](http://www.gotw.ca/publications/concurrency-ddj.htm)。从最低端的手机到最耗电的服务器，硬件的并行性以[更多、更慢、堆砌 cpu 内核](https://www.technologyreview.com/s/601441/moores-law-is-dead-now-what/)的形式出现，但只有**当**你的语言可以利用它们才有意义。因此，并发特性需要内置到我们编写的要在今天的硬件上运行的软件中。


通过提供一种基于协程的[轻量级并发模型](https://blog.golang.org/concurrency-is-not-parallelism)，或者是 Go 中已知的 goroutines，Go 超越了那些暴露操作系统的多进程或多线程并行模型的语言。goroutines 允许程序员避开复杂的回调，而语言运行时确保有足够的线程来保持你的内核的活跃。


### 总结


我给朋友推荐 Go 有三个原因：安全性、生产力和并发性。有些语言可以涵盖一个也有可能是两个方面，但是这三个方面的结合使得 Go 成为主流程序员的绝佳选择。


### 相关文章：


1. [为什么 Go 和 Rust 不是竞争对手](https://dave.cheney.net/2015/07/02/why-go-and-rust-are-not-competitors)
2. [听听我在 OSCON 上谈 Go 语言性能](https://dave.cheney.net/2015/05/31/hear-me-speak-about-go-performance-at-oscon)
3. [我在 GopherChina 和 GopherCon Singapore 中的演讲](https://dave.cheney.net/2017/02/09/im-speaking-at-gopherchina-and-gophercon-singapore)
4. [压力测试你 Go 包](https://dave.cheney.net/2013/06/19/stress-test-your-go-packages)



---


via: <https://dave.cheney.net/2017/03/20/why-go>


作者：[Dave Cheney](https://dave.cheney.net/) 译者：[geekpi](https://github.com/geekpi) 校对：[jasminepeng](https://github.com/jasminepeng)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
A few weeks ago I was asked by a friend, “why should I care about Go”? They knew that I was passionate about Go, but wanted to know why I thought *other people* should care. This article contains three salient reasons why I think Go is an important programming language.

# Safety

As individuals, you and I may be perfectly capable of writing a program in C that neither leaks memory or reuses it unsafely. However, with more than [40 years](https://en.wikipedia.org/wiki/C_(programming_language)) of experience, it is clear that programmers working in C are unable to reliably do so *en masse*.

Despite tools such as static code analysis, valgrind, tsan, and `-Werror`

being available for a decades, there is scant evidence those tools have achieved widespread acknowledgement, let alone widespread adoption. In aggregate, programmers have shown they simply cannot safely manage their own memory.

Go does not rely on the programmer to manage memory directly, instead all memory allocation is managed by the language runtime, initialized before use, and bounds checked when necessary. It’s certainly not the first mainstream language that offered these safety guarantees, Java (1995) is probably a contender for that crown. The point being, the world has no appetite for unsafe programming languages, thus Go is memory safe by default.

# Developer productivity

The point at which developer time became more expensive than hardware time was crossed back in the late 1970s. Developer productivity is a sprawling topic but it boils down to this; how much time do you spend doing useful work vs waiting for the compiler or hopelessly lost in a foreign codebase.

The joke goes that Go was developed while waiting for a [C++ program to compile](https://commandcenter.blogspot.com.au/2012/06/less-is-exponentially-more.html). Fast compilation is a key feature of Go and a key recruiting tool to attract new developers. While compilation speed remains a [constant battleground](https://dave.cheney.net/2016/11/19/go-1-8-toolchain-improvements), it is fair to say that compilations which take minutes in other languages, take seconds in Go.

More fundamental to the question of developer productivity, Go programmers realise that code is *written to be read* and so place the [act of reading code above the act of writing it](https://twitter.com/rob_pike/status/791326139012620288). Go goes so far as to enforce, via tooling and custom, that all code by formatted in a specific style. This removes the friction of learning a project specific language sub-dialect and helps spot mistakes because they just *look incorrect.*

Due to a focus on analysis and mechanical assistance, a growing set of tools that exist to spot common coding errors have been adopted by Go developers in a way that never struck a chord with C programmers—Go developers *want* tools to help them keep their code clean.

# Concurrency

For more than a decade, chip designers have been warning that the [free lunch is over](http://www.gotw.ca/publications/concurrency-ddj.htm). Hardware parallelism, from the lowliest mobile phone to the most power hungry server, in the form of [more, slower, cpu cores](https://www.technologyreview.com/s/601441/moores-law-is-dead-now-what/), is only available *if* your language can utilise them. Therefore, concurrency needs to be built into the software we write to run on today’s hardware.

Go takes a step beyond languages that expose the operating system’s multi-process or multi-threading parallelism models by offering a [lightweight concurrency model based on coroutines](https://blog.golang.org/concurrency-is-not-parallelism), or goroutines as they are known in Go. Goroutines allows the programmer to eschew convoluted callback styles while the language runtime makes sure that there will be just enough threads to keep your cores active.

# The rule of three

These were my three reasons for recommending Go to my friend; safety, productivity, and concurrency. Individually, there are languages that cover one, possibly two of these domains, but it is the combination of all three that makes Go an excellent choice for mainstream programmers today.