学习 Golang 的 4 个技巧
Golang

> 
> 到达 Golang 大陆：一位资深开发者之旅。
> 
> 
> 


![](/data/attachment/album/201811/24/122139dqbvftbwdqcqu7l0.jpg)


2014 年夏天……



> 
> IBM：“我们需要你弄清楚这个 Docker。”
> 
> 
> 我：“没问题。”
> 
> 
> IBM：“那就开始吧。”
> 
> 
> 我：“好的。”（内心声音）：”Docker 是用 Go 编写的。是吗？“（Google 一下）“哦，一门编程语言。我在我的岗位上已经学习了很多了。这不会太难。”
> 
> 
> 


我的大学新生编程课是使用 VAX 汇编程序教授的。在数据结构课上，我们使用 Pascal —— 在图书馆计算机中心的旧电脑上使用软盘加载。在一门更高一级的课程中，我的教授教授喜欢用 ADA 去展示所有的例子。在我们的 Sun 工作站上，我通过各种 UNIX 的实用源代码学到了一点 C。在 IBM，OS/2 源代码中我们使用了 C 和一些 x86 汇编程序；在一个与 Apple 合作的项目中我们大量使用 C++ 的面向对象功能。不久后我学到了 shell 脚本，开始是 csh，但是在 90 年代中期发现 Linux 后就转到了 Bash。在 90 年代后期，我在将 IBM 的定制的 JVM 代码中的即时（JIT）编译器移植到 Linux 时，我不得不开始学习 m4（与其说是编程语言，不如说是一种宏处理器）。


一晃 20 年……我从未因为学习一门新的编程语言而焦灼。但是 [Go](https://golang.org/) 让我感觉有些不同。我打算公开贡献，上传到 GitHub，让任何有兴趣的人都可以看到！作为一个 40 多岁的资深开发者的 Go 新手，我不想成为一个笑话。我们都知道程序员的骄傲，不想丢人，不论你的经验水平如何。


我早期的调研显示，Go 似乎比某些语言更 “地道”。它不仅仅是让代码可以编译；也需要让代码可以 “Go Go Go”。


现在，我的个人的 Go 之旅四年间有了几百个拉取请求（PR），我不是致力于成为一个专家，但是现在我觉得贡献和编写代码比我在 2014 年的时候更舒服了。所以，你该怎么教一个老人新的技能或者一门编程语言呢？以下是我自己在前往 Golang 大陆之旅的四个步骤。


### 1、不要跳过基础


虽然你可以通过复制代码来进行你早期的学习（谁还有时间阅读手册！？），Go 有一个非常易读的 [语言规范](https://golang.org/ref/spec)，它写的很易于理解，即便你在语言或者编译理论方面没有取得硕士学位。鉴于 Go 的 **参数：类型** 顺序的特有习惯，以及一些有趣的语言功能，例如通道和 go 协程，搞定这些新概念是非常重要的是事情。阅读这个附属的文档 [高效 Go 编程](https://golang.org/doc/effective_go.html)，这是 Golang 创造者提供的另一个重要资源，它将为你提供有效和正确使用语言的准备。


### 2、从最好的中学习


有许多宝贵的资源可供挖掘，可以将你的 Go 知识提升到下一个等级。最近在 [GopherCon](https://www.gophercon.com/) 上的所有讲演都可以在网上找到，如这个 [GopherCon US 2018](https://tqdev.com/2018-gophercon-2018-videos-online) 的详尽列表。这些讲演的专业知识和技术水平各不相同，但是你可以通过它们轻松地找到一些你所不了解的事情。[Francesc Campoy](https://twitter.com/francesc) 创建了一个名叫 [JustForFunc](https://www.youtube.com/channel/UC_BzFbxG2za3bp5NRRRXJSw) 的 Go 编程视频系列，其不断增多的剧集可以用来拓宽你的 Go 知识和理解。直接搜索 “Golang" 可以为那些想要了解更多信息的人们展示许多其它视频和在线资源。


想要看代码？在 GitHub 上许多受欢迎的云原生项目都是用 Go 写的：[Docker/Moby](https://github.com/moby/moby)、[Kubernetes](https://github.com/kubernetes/kubernetes)、[Istio](https://github.com/istio/istio)、[containerd](https://github.com/containerd/containerd)、[CoreDNS](https://github.com/coredns/coredns)，以及许多其它的。语言纯粹主义者可能会认为一些项目比另外一些更地道，但这些都是很好的起点，可以看到在高度活跃的项目的大型代码库中使用 Go 的程度。


### 3、使用优秀的语言工具


你会很快了解到 [gofmt](https://blog.golang.org/go-fmt-your-code) 的宝贵之处。Go 最漂亮的一个地方就在于没有关于每个项目代码格式的争论 —— **gofmt** 内置在语言的运行环境中，并且根据一系列可靠的、易于理解的语言规则对 Go 代码进行格式化。我不知道有哪个基于 Golang 的项目会在持续集成中不坚持使用 **gofmt** 检查拉取请求。


除了直接构建于运行环境和 SDK 中的一系列有价值的工具之外，我强烈建议使用一个对 Golang 的特性有良好支持的编辑器或者 IDE。由于我经常在命令行中进行工作，我依赖于 Vim 加上强大的 [vim-go](https://github.com/fatih/vim-go) 插件。我也喜欢微软提供的 [VS Code](https://code.visualstudio.com/)，特别是它的 [Go 语言](https://code.visualstudio.com/docs/languages/go) 插件。


想要一个调试器？[Delve](https://github.com/derekparker/delve) 项目在不断的改进和成熟，它是在 Go 二进制文件上进行 [gdb](https://www.gnu.org/software/gdb/) 式调试的强有力的竞争者。


### 4、写一些代码


你要是不开始尝试使用 Go 写代码，你永远不知道它有什么好的地方。找一个有 “需要帮助” 问题标签的项目，然后开始贡献代码。如果你已经使用了一个用 Go 编写的开源项目，找出它是否有一些可以用初学者方式解决的 Bug，然后开始你的第一个拉取请求。与生活中的大多数事情一样，实践出真知，所以开始吧。


事实证明，你可以教会一个资深的老开发者一门新的技能甚至编程语言。




---


via: <https://opensource.com/article/18/11/learning-golang>


作者：[Phill Estes](https://opensource.com/users/estesp) 选题：[lujun9972](https://github.com/lujun9972) 译者：[dianbanjiu](https://github.com/dianbanjiu) 校对：[wxy](https://github.com/wxy)


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


OK
In the summer of 2014...

IBM: "We need you to go figure out this Docker thing."


Me: "OK."

IBM: "Start contributing and just get involved."

Me: "OK." (internal voice): "This is written in Go. What's that?" (Googles) "Oh, a programming language. I've learned a few of those in my career. Can't be that hard."

My university's freshman programming class was taught using VAX assembler. In data structures class, we used Pascal—loaded via diskette on tired, old PCs in the library's computer center. In one upper-level course, I had a professor that loved to show all examples in ADA. I learned a bit of C via playing with various Unix utilities' source code on our Sun workstations. At IBM we used C—and some x86 assembler—for the OS/2 source code, and we heavily used C++'s object-oriented features for a joint project with Apple. I learned shell scripting soon after, starting with csh, but moving to Bash after finding Linux in the mid-'90s. I was thrust into learning m4 (arguably more of a macro-processor than a programming language) while working on the just-in-time (JIT) compiler in IBM's custom JVM code when porting it to Linux in the late '90s.


Fast-forward 20 years... I'd never been *nervous* about learning a new programming language. But [Go](https://golang.org/) felt different. I was going to contribute publicly, upstream on GitHub, visible to anyone interested enough to look! I didn't want to be the laughingstock, the Go newbie as a 40-something-year-old senior developer! We all know that programmer pride that doesn't like to get bruised, no matter your experience level.

My early investigations revealed that Go seemed more committed to its "idiomatic-ness" than some languages. It wasn't just about getting the code to compile; I needed to be able to write code "the Go way."

Now that I'm four years and several hundred pull requests into my personal Go journey, I don't claim to be an expert, but I do feel a lot more comfortable contributing and writing Go code than I did in 2014. So, how do you teach an old guy new tricks—or at least a new programming language? Here are four steps that were valuable in my own journey to Golang land.

## 1. Don't skip the fundamentals

While you might be able to get by with copying code and hunting and pecking your way through early learnings (who has time to read the manual?!?), Go has a very readable [language spec](https://golang.org/ref/spec) that was clearly written to be read and understood, even if you don't have a master's in language or compiler theory. Given that Go made some unique decisions about the order of the **parameter:type** constructs and has interesting language features like channels and goroutines, it is important to get grounded in these new concepts. Reading this document alongside [Effective Go](https://golang.org/doc/effective_go.html), another great resource from the Golang creators, will give you a huge boost in readiness to use the language effectively and properly.

## 2. Learn from the best

There are many valuable resources for digging in and taking your Go knowledge to the next level. All the talks from any recent [GopherCon](https://www.gophercon.com/) can be found online, like this exhaustive list from [GopherCon US in 2018](https://tqdev.com/2018-gophercon-2018-videos-online). Talks range in expertise and skill level, but you can easily find something you didn't know about Go by watching the talks. [Francesc Campoy](https://twitter.com/francesc) created a Go programming video series called [JustForFunc](https://www.youtube.com/channel/UC_BzFbxG2za3bp5NRRRXJSw) that has an ever-increasing number of episodes to expand your Go knowledge and understanding. A quick search on "Golang" reveals many other video and online resources for those who want to learn more.

Want to look at code? Many of the most popular cloud-native projects on GitHub are written in Go: [Docker/Moby](https://github.com/moby/moby), [Kubernetes](https://github.com/kubernetes/kubernetes), [Istio](https://github.com/istio/istio), [containerd](https://github.com/containerd/containerd), [CoreDNS](https://github.com/coredns/coredns), and many others. Language purists might rate some projects better than others regarding idiomatic-ness, but these are all good starting points to see how large codebases are using Go in highly active projects.

## 3. Use good language tools

You will learn quickly about the value of [gofmt](https://blog.golang.org/go-fmt-your-code). One of the beautiful aspects of Go is that there is no arguing about code formatting guidelines per project—**gofmt** is built into the language runtime, and it formats Go code according to a set of stable, well-understood language rules. I don't know of any Golang-based project that doesn't insist on checking with **gofmt** for pull requests as part of continuous integration.

Beyond the wide, valuable array of useful tools built directly into the runtime/SDK, I strongly recommend using an editor or IDE with good Golang support features. Since I find myself much more often at a command line, I rely on Vim plus the great [vim-go](https://github.com/fatih/vim-go) plugin. I also like what Microsoft has offered with [VS Code](https://code.visualstudio.com/), especially with its [Go language](https://code.visualstudio.com/docs/languages/go) plugins.

Looking for a debugger? The [Delve](https://github.com/derekparker/delve) project has been improving and maturing and is a strong contender for doing [gdb](https://www.gnu.org/software/gdb/)-like debugging on Go binaries.

## 4. Jump in and write some Go!

You'll never get better at writing Go unless you start trying. Find a project that has some "help needed" issues flagged and make a contribution. If you are already using an open source project written in Go, find out if there are some bugs that have beginner-level solutions and make your first pull request. As with most things in life, the only real way to improve is through practice, so get going.

And, as it turns out, apparently you can teach an old senior developer new tricks—or languages at least.

## 4 Comments