Go language quick start graphic tutorial

Time:2021-7-28

Recommended reading:

The latest version of go language activation tutorial canClick this link to view

GoLand permanent installation tutorial,Click here to view

Go has been very popular in recent years. Xiaoha has also rubbed Mimi to learn in his spare time (my big Java is still invincible). Although the company’s technology stack is not available for the time being, it is guaranteed that there will be new project selection go behind, and he can work directly at that time.

We still need some technical reserves. We should make secret efforts and surprise everyone. Ha ha ~ don’t talk too much nonsense. Let’s do it!

1、 What is go language?

Go (also known as golang) is a static, compiled, and hairstyle language developed by Google with garbage collection function.

Robert grishmore, rob pike and Ken Thompson began to design the go language in September 2007. The original intention of the design is to improve the development efficiency in the case of multi-core, networked machines and large code bases. At that time, at Google, designers wanted to remove the current shortcomings of the mainstream programming languages and only absorb their advantages for the go language.

Go was officially open source in November 2009 and supports Linux, MacOS, windows and other operating systems.

In 2016, go language was named “language of the year” by tiobe.

2、 Go language features

  • Go language is very easy to get started. Its style is similar to C language, but its syntax is greatly simplified on the basis of C language. For example, redundant expression brackets are removed, and loop traversal is only limitedforA writing method, etc;
  • Simple engineering structure;
  • High code readability and ease of use;
  • The compilation speed is fast. Due to its simple syntax, rigorous engineering structure design, no header file and no package cross dependency, it saves the time required for compilation to a great extent.
  • High performance, native support concurrency. Concurrency based on go languagegoroutine, it is similar to a thread, but not a thread. When the go language runs, it will participate in schedulinggoroutine, andgoroutineReasonably allocate to each CPU, so as to ensure the CPU utilization to the greatest extent.
  • Rich and powerful standard library, including network, graphics, system, encryption, coding and other aspects.

3、 How popular is the go language?

Go language is known as the C language in the Internet era, so how popular is it?

1. Most cloud native projects are written in go language, such as:

  • Docker: container technology;
  • Kubernetes: container scheduling service built on docker;
  • Etcd: a distributed and reliable kV storage system;
  • Istio: an open platform for service governance in the form of service mesh for cloud native scenarios closely combined with kubernetes;
  • Prometheus: an open source service monitoring system and time series database;

2. Go language will become the infrastructure programming language in the cloud computing era, such as blockchain star projects hyperledger and newsql, and star project tidb is also based on go language;

3. Go language has become the next enterprise programming language, which is used by front-line Internet companies including byte beating, Didi, Tencent and Alibaba.

4、 Download go installation package

Want to learnGo languageFirst, you have to download the go language installation package. You can download it from the following two websites:

Golang China:https://golang.google.cn/dl/

Go language official website:https://golang.org/dl/

Tip: the go language official website can only be accessed through scientific Internet. It is recommended to download the first link.

After successful access, as shown in the figure below, select the corresponding go language installation package according to your own system, and click download.

After downloading the corresponding system installation package, please refer to the installation instructions in subsequent chapters:

If you choose to build from the source code, you can refer toOfficial build documentation

In addition, for the historical version of go language, see:http://docs.studygolang.com/doc/devel/release.html

Relevant description

file name explain
go1.15.3.windows-amd64.msi Windows platform installation package
go1.15.3.linux-amd64.tar.gz Linux platform installation package
go1.15.3.darwin-amd64.pkg Maxos platform installation package
go1.15.3.src.tar.gz Go language source package, for research, daily development is not recommended to download

5、 Install go locale

Windows setup go

In this section, xiaoha will demonstrate how to install on Windows systemGo languageenvironment

PS: what you need to know about the installation package name:

1.15.4 indicates the version number of the current go installation package;

Windows indicates that this is the installation package of windows system;

AMD64 indicates the CPU matching 64 bits;

After downloading, double-click the installation package to start installation:

clickNext

Here, the default installation path of go development package isC:\Go\, this default path is recommended.

After the installation, we can go toC:\Go\Under directory, look at the directory structure:

Go installation directory description

Directory name explain
api API change differences between versions
bin Go source package compiled compiler, formatting tools, documentation tools, etc
doc Go document in English
lib Dependent library files
misc Relevant files for other purposes, such as compilation of Android platform, etc
pkg Intermediate files compiled on Windows platform
src Standard library source code
test test case

Check whether the windows system go environment is successfully installed

opencmdWindow, execute the command:


go version

If the version number of go language is correctly output, the installation is successful:

Linux Installation go

Here, xiaoha demonstrates the ECS CentOS 7 he purchased.

After downloading, upload the compressed package to the specified directory and execute the following command to decompress the compressed package to/usr/local/goBelow:


tar -C /usr/local -xzf go1.15.4.linux-amd64.tar.gz

After decompression, open the profile file through the VI editor:


vi /etc/profile

stayprofileAdd the following line to the file to/usr/local/go/binAdd directory toPATHRemove from environment variables:


export PATH=$PATH:/usr/local/go/bin

Then execute the following command to make the newly added environment variable take effect:


source /etc/profile

After completion, execute the following command to check whether the go language is successfully installed:


go version

If the go language version number is correctly output, the installation is successful.

Mac install go

The MAC system has the following two installation methods.

Install via brew

Execute the following command:


brew install go

perhaps


brew install golang

In addition, you can also passbrew info goCheck the version information.

Download the go language PKG package from the official website to install it

When the download is complete, double-click to start the installation:

clickcontinue, wait for the go installation to complete.

Configure go locale variables

Go is installed by default/usr/local/goNext, start configuring the go localevariable

1. Edit.bash_profileFile:


vim ~/.bash_profile

2. Add/usr/local/goDirectory toPATHIn the environment variable:

#Go environment variable
export PATH=$PATH:/usr/local/go/bin

3. Execute the following command to make the environment variable effective:


source ~/.bash_profile

Verification results

Verify whether the go locale is successfully installed by using the following command:


go version

If the version number of go language is correctly output, it indicates success.

6、 Write a hello world program

Generally speaking, the first program written in any language is to say hello to the world in that language: Hello world!

start

Next, let’s write the first oneGo languageProgram, create a new onehelloworld.goFile, code as follows:

Tip: the extended name of the go language source file is.goending.


package main

import "fmt"

func main() {
    fmt.Println("Hello World !")
}

The above code means to print on the consolecharacter stringHello World !

Execute go procedure

We have two ways to execute the above program.

The first way

adoptgo runCommand to execute the code just. The command is as follows:


go run helloworld.go 

The renderings are as follows:

The second way

You can also executego buildCommand to compile this code into an executable file:


go build helloworld.go

After compiling, you can see a directoryhelloworldExecutable, via./helloworldCommand to execute it, you can outputHello World !

7、 Conclusion

This article mainly talks about how to quickly get started with the go language and play a role in attracting jade. If this article can be useful to my friends, I hope my friends can make a wave three times in a row!

The above is the details of the go language quick start graphic tutorial. For more information about the go language tutorial, please pay attention to other relevant articles of developeppaer!

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]