Go Language Overview

Language history

Go language, also known as Golang, is a statically strongly typed, compiled, language native support concurrency, garbage collection programming language developed by Google. Originated in 2007 and officially released in 2009. Go is a very young language whose primary goal is to “combine the speed of a dynamic language like Python with the performance and security of a compiled language like C/C++.”

Go is another attempt at programming language design, a major improvement over the C-like language, which not only gives you access to the underlying operating system, but also provides powerful support for network programming and concurrent programming. Go language has many uses, can be network programming, system programming, concurrent programming and so on.

The launch of the Go language, aimed at reducing the complexity of the code without losing the performance of the application, with “simple deployment, good concurrency, good language design, good execution performance” and other advantages.

Go is sometimes described as “the C language of the 21st century”. Go inherits similar expression syntax, control flow structure, basic data types, call parameter passing, Pointers and many other ideas from C language, as well as the efficiency of C language after compilation.

Go does not have the concepts of classes and inheritance. It implements code reuse through composition, and it implements polymorphism through the concept of interfaces. So object-oriented programming in Go is different from traditional object-oriented languages such as C++ and Java.

Go has a mascot, and conferences, document pages, and blog posts mostly include Go Gopher, as shown below, designed by talented illustrator Renee French, wife of Go Designer Rob Pike.

Language features

Grammar is simple

The Go language is designed with a similar philosophy to Unix’s “less is more.” The grammatical rules of Go are rigorous and unambiguous, which makes it easy to learn. The Go language preserves Pointers, but generally disallows pointer operations (preserving the ability of the unsafe package to manipulate Pointers). In addition, Go has built-in slicing and dictionaries that improve development efficiency while preserving runtime performance.

Concurrency is supported at the language level

The main concurrency models are multi-process model and multi-thread model. Different from the mainstream multi-concurrent model, Go language adopts the implementation of coroutine based on CSP, and makes more in-depth optimization at runtime. This makes concurrent programming at the language level extremely easy, without having to deal with callbacks or focus on thread switching, just by adding the go keyword.

“Through communication to shared memory, but not through shared memory to communicate”, go language built-in channel data structure with go keyword to achieve concurrent communication and control, this is a good solution for the multi-threaded model that needs to consider memory visibility and other problems.

Efficient garbage collection

With every update to the Go language, the garbage collector is inevitably the most changed part of the core component. Everything from concurrent cleanup, to lowering STW times, until version 1.5 of Go implemented concurrent tagging, and gradually introduced tricolor tagging and write barriers to make garbage collection work better without impacting user logic. From the initial second level STW to the current microsecond level STW, the Go language development team has been working on garbage collection.

Static link

The benefits of static compilation are obvious. Packaging runtime and dependent libraries directly into the executable simplifies deployment and distribution without the need to install a runtime environment beforehand and download many third-party libraries. Although it increases the size of executable files compared to dynamic compilation, it eliminates the management of dependent libraries. With the development of microservices and containerization, this has become one of the killer features of the Go language, where a single binary file can run a service.

The standard library

A full-featured and reliable standard library provides powerful support for programming languages. Most basic functionality can be developed without third-party extensions, which greatly reduces learning and use costs.

The Go standard library is extremely rich. One of the things that deserves a lot of credit is NET/HTTP, a high-performance Web Server that can be implemented with just a few simple statements.

Tool chain

A complete toolchain is critical to project development. Go does a pretty good job of this, with tools for compilation, formatting, error checking, help documentation, and third-party package downloads and updates.

It is worth mentioning that gofMT tool, in order to solve the problem of “inconsistent code style” often encountered by developers, the official goFMT directly designated a set of standards, it can be seen that go language does solve many practical problems in engineering.

In addition, Go has a complete testing framework built into it, including unit testing, performance testing, code coverage, data competition, and pPROF for tuning, all of which are essential tools to ensure correct and stable running of code.

Application scenarios of Go

Since the release of version 1.0, Go language has attracted the attention of many developers and has been widely used. The simple, efficient and concurrent features of Go language have attracted many traditional language developers to join in, and the number is increasing.

In view of the characteristics and original design of Go language, Go language as a server programming language, is very suitable for processing logs, data packaging, virtual machine processing, file system, distributed system, database agent, etc. In network programming, Go language is widely used in Web applications, API applications, download applications, etc. In addition, Go language is also applicable to the field of in-memory database and cloud platform. At present, many foreign cloud platforms are developed by Go.

  • Server programming. For example, log processing, data packaging, virtual machine processing, file system, and so on.

  • Distributed systems, database agents, middleware, etc. Such as Etcd.

  • Network programming. This is by far the most widely used segment, including Web applications, API applications, download applications, and so on.

  • Develop the cloud platform. At present, many domestic and foreign cloud platforms are using Go development.

Go language well-known project

After the release of Go, many companies, especially cloud computing companies, began to reconstruct their infrastructures with Go. Many infrastructures were directly developed with Go, resulting in many hot projects.

infrastructure

Representative projects: Docker, Kubernetes, ETCD, Consul, etc.

The database

Representative projects: InfluxDB, CockroachDB, etc.

Micro service

Representative projects: Go-Kit, Micro, Kratos, etc.

Install Go

Go is available on FreeBSD, Linux, Windows, and macOS operating systems. For requirements for these platforms, please participate in the system requirements listed on the Go language website.

The official website for Go is golang.org/, and users in China can visit h…

Windows system

Download link

  • Download address: 32-bit golang. Google. Cn/dl/go1.15.8…

  • 64 download address: golang. Google. Cn/dl/go1.15.8…

The default installation directory is C:\go. Do not change the installation directory.

GOPATH configuration

After the installation is complete, you need to configure GOPATH. GOPATH is the path where Go language stores third-party source code, binary files, and class libraries.

  1. For example, if the system user name is Demo, create the following directories:
  • C:\Users\demo\go\ SRC store source code

  • C:\Users\demo\go\ PKG store the class library

  • C:\Users\demo\go\bin Binary files exist

  1. Environment variable Settings:
  • Add GOPATH with value C:\Users\demo\go

  • Add PATH (edit if it already exists) to C:\Users\demo\go\bin

Linux system

There are many distributions of Linux, such as Ubuntu, CentOS, RedHat, Debian, and so on, and the installation steps are the same for all distributions. The difference is that you choose a different distribution depending on your CPU architecture.

The COMMON PC CPU architecture is AMD64. You can download the AMD64 architecture release package.

Linux Configuration Commands

Download the zip packageWget HTTP: / / https://golang.google.cn/dl/go1.15.8.linux-amd64.tar.gzMove to the opt directoryThe mv go1.15.8. Linux - amd64. Tar. Gz/opt# decompressionThe tar xf go1.15.8. Linux - amd64. Tar. GzCreate a GOPATH directory
cd ~
mkdir go
cd go
mkdir pkg src bin
# Edit ~/. Bashrc file and add the bin PATH to the PATH environment variable
echo 'GOPATH= user home directory /go' >> ~/.bashrc
echo 'PATH=/opt/go/bin:$GOPATH/bin:$PATH' >> ~/.bashrc
Update environment variables
source ~/.bashrc
Test the installation results
go version
Copy the code

MacOS systems

Apple released hardware products using M1 chip (ARM64 architecture) in 2020, and the Go language version supporting M1 chip is 1.16. You can install the corresponding PKG package according to the CPU architecture.

  • Amd64: golang. Google. Cn/dl/go1.15.8…

  • Arm64: golang. Google. Cn/dl/go1.16. D…

MacOS configuration commands

Create a GOPATH directory
cd ~
mkdir go
cd go
mkdir pkg src bin
# Edit ~/. Bashrc file and add the bin PATH to the PATH environment variable
echo 'GOPATH= user home directory /go' >> ~/.bashrc
echo 'PATH=$GOPATH/bin:$PATH' >> ~/.bashrc
Update environment variables
source ~/.bashrc
Test the installation results
go version
Copy the code

Configure the integrated development environment

This section describes how to configure the INTEGRATED development environment (IDE) on your local computer. The following steps use the macOS version as an example.

Visual Studio Code(referred to as VSCode) is an open source editor developed by Microsoft that supports Windows, Linux and macOS operating systems. It supports testing, has built-in git function, and provides rich language support and common programming tools.

  1. Open the official website code.visualStudio.com/, click the blue button to download…

  2. The new version of VSCode does not have the built-in Chinese language pack, so you need to install the language pack extension. After installing VSCode, open the VSCode editor, search for “Chinese” in the extension window, and install the first one.

  1. Open the project and create main.go. At this time, the go tool chain installation prompt will pop up in the lower right corner of VSCode, select “Install All”.

Writing an HTTP server

code

package main

import (
	"io"
	"net/http"
)

func main(a) {
	http.HandleFunc("/".func(w http.ResponseWriter, r *http.Request) {
		io.WriteString(w, "hello world")
	})
	http.ListenAndServe(": 8080".nil)}Copy the code

Program structure specification

  • The package keyword declares the package in which the file resides, which must be declared for every GO file. Each executable must contain the main package, and the entry point of the program is the func main function of the main package
  • The import keyword declares the package to be imported. HTTP packages are imported because HTTP server related methods are required in the code
  • The entry point of func main

Compile and run the program

Compiling and running files is a common step in the development process, and Go provides a quick way to do this.

The Go language provides two commands to compile and run Go programs:

  • Go Build compiles the executable, but does not execute it
  • Go Run does not create an executable file but executes it directly

Run the HTTP server using Go Run and then open it in a browser.

summary

This article describes the Go language installation and integrated development environment configuration. The development process of Go program is demonstrated through HTTP server.

The next chapter will learn the basic syntax of the Go language:

  • Variables and constants
  • The data type
  • The operator
  • Conditional statements
  • Looping statements