preface

Monkey has moved to a new company that uses Go as its development language, but has given up Java after five years to make a living. However, as a technical programmer, keep track of your language gains and losses. After all, it’s never too late to learn a new language. I also learn while writing, if there is fallacy, please correct.

Introduction of the Go

Each new language is created to address the shortcomings of the existing language, and the Go project is a reflection on some of the problems encountered in maintaining several super-complex software systems at Google.

This is an excerpt from the Go language Bible. It can be seen that Go inherits the gene of C, including ideas such as basic data types and Pointers. Therefore, I think it will be very easy to learn Go if you have the basis of C before. It also includes concepts related to package handling from the Pascal language family, while CSP’s handling of pipes brings important features that differentiate GO from other languages.

Also GO personally, I agree with simple programming philosophy is multiplication level related to the complexity of the software, write complex logic to solve complicated difficulty is not high, but by simple logic to complete processing is a very complicated problem, test a thing of the technical level of the programmer, but for the future operations continuously optimize and very important.

Environment set up

The first thing you need to do to learn a language is to download the corresponding binary package from the official website according to your computing architecture and operating system.

For Go, we can download the installation package at golang.org/dl/. If not, use the official mirror address: golang.google.cn/dl/.

Using my MAC as an example, unpack the downloaded binary package into /usr/local and configure the appropriate environment variables. Or download the executable version. There are so many tutorials online that I won’t go into them here. After the installation is complete, run the go version command to check whether the installation is successful.

Environment configuration

In this process, we also need to configure three environment variables, namely GOROOT, GOPATH, and GOBIN. Today’s presentation focuses on these three environment variables:

GOROOT: Installation path of the GO language.

GOPATH: Our self-defined workspace.

GOBIN: path to the executable file generated by the GO program.

1. GO project structure These three variables are closely related to the GO language project structure:

When we were developing Go, our code was always stored in the GOPATH/ SRC directory. After the project goes through go build, Go Install, or Go get commands, the downloaded third-party package source files will be placed in the GOPATH/ SRC directory, and the generated binary executable files will be placed in the GOPATH/bin directory. The generated intermediate cache files are stored under GOPATH/ PKG.

You can think of GOPATH simply as the Go language’s working directory. Its value is the path to a directory or multiple directory paths, each representing a workspace for the Go language.

As with many programming languages, Go’s source code is organized as a basic unit of code packages. In the file system, these code packages actually correspond to directories one by one. Because directories can have subdirectories, code packages can also have subpackages.

Therefore, the Go language source code is organized by environment variables GOPATH, workspace, SRC directory and code package as the main line.

2. The workspace

The value of GOPAT can be multiple directory paths, separated by colons on Linux and semicolons on Windows. If you use Go Get, the first GOPATH path will be installed by default

There are two things to note here:

  1. In what order does the Go language find dependency packages in multiple workspaces?

It’s actually the value of GOPATH that we set that determines the order, and Go will traverse from left to right according to the workspace that we set.

  1. Will there be conflicts if there are code packages with the same import path in multiple workspaces?

There is no conflict because the code package lookup is done one by one in multiple workspaces in a given order.

3. Build and install the GO program

The SRC directory under GOPATH is the main directory for the next development program. All the source code is placed under this directory, so we usually do one directory per project

/ / $GOPATH/SRC/mymath/SQRT. Go the source code is as follows:
package mymath

func Sqrt(x float64) float64 {
	z := 0.0
	for i := 0; i < 1000; i++ {
		z -= (z*z - x) / (2 * x)
	}
	return z
}
Copy the code

My application package directory and code have been created. Note: It is generally recommended that the name of the package be the same as the directory name

Build using the go build command, install using the go install command. Compilation, packaging, and so on are performed when code packages are built and installed, and any files generated by these operations are first saved to a temporary directory.

If you are building a library file, the resulting file will only exist in the temporary directory. The main point of the build here is to check and validate.

If you are building a command file (that is, a mian package with a main function), the resulting file will be moved to the same directory as the source file.

The install operation performs the build first, then links, and moves the resulting file to the specified directory.

Further, if a library file is installed, the resulting file is moved to a subdirectory under the PKG directory of its workspace.

There are two ways to install it

1. Go to the corresponding application package directory and run Go Install

2. Run the following code in any directory: go install myMath

After installation, we can go to the following directory:

cd $GOPATH/pkg/${GOOS}_${GOARCH}
// You can see the following files
mymath.a
Copy the code

If a command file is installed, the result file is moved to the bin directory of its workspace, or to the directory pointed to by the environment variable GOBIN.

This dot a file is the application package, so how do we call it?

Next we create a new application to invoke the application package

Create the application package MathApp

package main

import (
	"mymath"
	"fmt"
)

func main(a) {
	fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))}Copy the code

This is the path relative to GOPATH/ SRC. If it’s a multilevel directory, introduce multilevel directories in import. If you have multiple GOPATH, the same goes for import. Go is automatically found in multiple $GOPATH/ SRC.

How do you compile a program? Go to the application directory and execute Go Build, which generates a MathApp executable file under the directory

./mathapp
Copy the code

Output the following

Hello, world.  Sqrt(2) = 1.414213562373095
Copy the code

How do I install the application? Add mathApp to GOPATH/bin/. Add GOPATH/bin to GOPATH/bin/

mathapp
Copy the code

Also output the following

Sqrt(2) = 1.414213562373095Copy the code

conclusion

The concept and meaning of workspaces and GOPATH is something every Go engineer needs to understand. While they are relatively simple, it is fair to say that they are the core knowledge of Go program development.

Related to recommend

GO Language Bible shouce.jb51.net/gopl-zh/ind… The GO language core 36 speak time.geekbang.org/column/intr…