In the era of cloud computing, Go is the programming language of choice, and much of the cloud infrastructure is written by Go, not to mention big hitters like Docker and Kubernetes. For back-end engineers, this is the most valuable skill to learn.

This article is for people with a certain level of foundation, because Go is not for beginners, and for beginners, it is recommended to start with Python.

If you have a certain programming foundation, it will be much easier to learn go, especially if you have C language foundation, it will be much faster to learn GO language.

The syntax of Go is very small, and the key words are very concise. However, for those who have written the object-oriented language Java for a long time, they need to abandon the inherent idea of object-oriented in Java. Go provides a different scheme in object-oriented programming, without the concept of classes, and the interface is completely different from that in Java. For those familiar with Java, it takes some getting used to.

This article will not cover the specific tutorials of Go, but rather a path map, which I think is a good way to get started quickly with the language.

Before looking at the following, I would assume that you already know a development language, have done some Web development, and are familiar with the basics of Git.

Introduction to 1.

1.1 Getting Started

Environment configuration

Before writing code, you need to install Go. Go supports Linux, macOS, Windows and other platforms. You can download the installation package here.

MacOS and Windows provide binary installation packages, while Linux requires the source installation (Ubuntu, centos and other distributions can also be installed directly by command, but the latest version of Go is generally not available).

Before writing code, you need to understand two important concepts in Go: GOROOT and GOPATH. GOROOT is the Go installation directory. It can be understood as JAVA_HOME in Java.

GOPATH is usually a ~/go directory. Under this directory, there are usually three subdirectories: bin, PKG, and SRC. The bin directory contains executable commands, the PKG directory contains some middleware files generated during compilation, and the SRC directory contains the source code.

Before Go Modules (described below), Go dependency libraries and source code need to be placed in GOPATH’s SRC, otherwise the dependencies will not be found. After the advent of Go Modules, the Go dependency management problem is largely solved. You don’t need to put the source code in GOPATH, and GOPATH is basically only used to store the dependency.

Basic grammar

The official website of Go provides an introductory tutorial, A Tour of Go, which basically covers all the syntax of Go. You can access the app directly from the web or download it locally:

go get golang.org/x/tour
Copy the code

You can then type from the command line (GOPATH needs to be configured) :

tour
Copy the code

In this way, a local web page can be opened, and the loading speed will be much faster. After all, the official website of Go is in foreign countries, and direct access is still a little slow.

There is also a “Go By Example” tutorial. You can choose between these two tutorials.

Once you’ve done the above exercises, you can start reading the Go Programming Language, which is arguably the best introduction to Go and, while lagging behind in terms of content, is still well worth reading overall.

Common Command Tools

Go CLI commands are very important and need to be used throughout the Go development lifecycle.

The basic usage of the Go command is as follows:

go <command> [arguments]
Copy the code

In the early stages, the following commands must be mastered:

  • Go run: Compile and run the GO program

  • Go build: Compile and package the GO program

  • Go get: Installs the dependency

  • Go test: Run tests

The above command will be used frequently as you start to develop applications with Go. To view the complete command, type:

go help
Copy the code

After reading all these contents, I should be familiar with the basic syntax and common tools of GO. In order to use and get started with the development of GO as soon as possible, I should learn some common components and frameworks next.

1.2 Common Frameworks

If you’re going to develop an application, it’s going to involve Web API development, using MySQL and Redis. Here’s how to get up to speed on these frameworks in the Go language.

Web framework

There are many web frameworks that are commonly used in Go, and I recommend Gin, which is easy to learn and performs well.

go get -u github.com/gin-gonic/gin
Copy the code

You can quickly create an HTTP-based API service:

package main import "github.com/gin-gonic/gin" func main() { r := gin.Default() r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "Pong ",}) r.run () // Listen and serve on 0.0.0.0:8080 (for Windows "localhost:8080")}Copy the code

MySQL framework

In the Go language, the Database/SQL package defines all the interfaces for database operations, but does not provide the implementation. Because there are many types of databases, this only defines the interface of the shield database implementation, easy for us to replace and upgrade.

So when connecting to the database, need to choose the database driver, recommend those fully implemented database/ SQL driver, so conducive to the subsequent maintenance and upgrade of the code.

Take MySQL database as an example here, the commonly used driver is go-SQL-driver/MySQL, this driver fully implements the Database/SQL interface.

Easy to install:

$ go get -u github.com/go-sql-driver/mysql
Copy the code

You can then use the database/ SQL interface to manipulate the database:

import ( "database/sql" _ "github.com/go-sql-driver/mysql" ) db, err := sql.Open("mysql", "user:password@/dbname") if err ! = nil { panic(err) }Copy the code

Writing code using the native MySQL interface produces a lot of redundant code. If you want to make your code cleaner, you need to learn the GORM framework.

go get -u gorm.io/gorm
Copy the code

Redis framework

Using Redis in Go requires using the Go-Redis/Redis component. Installation is also simple:

go get github.com/go-redis/redis/v8
Copy the code

In addition to this component, another redigo use also quite a lot of people, you can explore.

Once you’ve learned this, you’re ready to use Go for everyday development.

2. Advanced

Once you’ve learned the basics, you need to learn more.

Go Modules

You’ll probably still be writing code in GOPATH when you learn the framework above. Since Go1.11, Go has launched Go Modules as an official dependency management tool.

This tool has to be learned. Dependency management has always been a weakness of the Go language, and after a number of unofficial solutions, the official launch of this tool was made. The best source of learning is the Go Blog: blog.golang.org/using-go-mo… . If the English version seems inconvenient, here is my translated version.

After using Go Modules, dependency management will be much more convenient, also do not need to develop in GOPATH.

Go Blog

The Go Blog is definitely an important resource for learning the Go language. Officially launched tools and features, will be published in the first time blog, look at these materials can understand the introduction of these features and tools background, will not understand the deviation.

For example, you can learn why Go’s declaration syntax is the way it is in the Go Blog.

Updates to the Go language features will be posted on the Go Blog, so following the Go Blog is a good opportunity to follow up on the Go features. After all, the Go language is still evolving, and there are many features that are constantly being updated.

Effective go

In addition to the Go Blog, there is also the Effective Go must-see document.

This document can be seen as a go-safe guide, pointing out the pitfalls of the Go language and helping you write cleaner, better-structured code. It’s short and well worth reading.

3. Other resources

Github is a great resource to learn to code today. There are plenty of resources to learn from in these Github repositories.

  • Avelino/Awesome go: Go’s wheel library is also getting better, and this Github repository is a collection of Go’s libraries, so be sure to check it out before you build a wheel

  • Alikhll/ Golang-developer-roadmap: Here’s another roadmap for Go developers

  • Talkgo/Read: This warehouse has a lot of Go tutorials, which are in Chinese, but the information needs to be sorted out, some of them are out of date

  • Go /wiki: Wiki for the Go project. There are a lot of technical documents and blogs. If you want to further study go, you can start here

4. Development tools

GoLand is also a tool produced by JetBrains, which requires a fee. If you don’t want to spend money on this, you can use VsCode, which has many plug-ins and is easy to install.

If you are willing to toss, you can try Vim, Vim with vim-Go plug-in, the experience is also good, is the installation process may step on some pits.

For the choice of computer development, I recommend Mac, which is still very suitable for development. At present, the standard configuration of development machine in Internet companies is Mac. If you think the price of new Mac is more expensive, you can choose second-hand or refurbished machine on the official website. If portability is not an issue, opt for the Mac Mini, which is cost-effective.

In the recent release of Go1.16, Go has started to support ARM architecture, so even macs with M1 chips can be used for Go development (but be careful with this, there may be other pits).

5. Summary

By the time you’ve gone through all of the above, you should be starting to learn Go, as I am now. Go is a very promising language, the first choice in the era of cloud computing, and well worth learning.

I will continue to update this article and this series of documents. If you have any suggestions, please let me know.

The text/Rayjun

This article is published on my wechat official account [Rayjun].