preface

For those of you who are just beginning to learn Go, there are a few concepts that may be confusing: GOROOT and GOPATH. Let’s talk about them today. Execute go env to show you the relevant parameters of my development environment (part) :

GOARCH="amd64"
GOBIN="/Users/chengguilu/goProjects/bin"
GOCACHE="/Users/chengguilu/Library/Caches/go-build"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/chengguilu/goProjects"
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
Copy the code

GOROOT

GOROOT represents the installation path for go compilation, tools, standard libraries, etc. (e.g. /usr/local/go).

GOPATH

This variable must be set before Go1.7 and cannot be the same as the Go installation directory. After 1.8, GOPATH has a default value of $HOME/go. Can perform the go env GOPATH view, from the above you can see, my GOPATH is/Users/chengguilu/goProjects. Basically, GOPATH corresponds to a directory on your computer. Function: Used to store Go code, Go runnable files, and corresponding compiled package files. The Go code must be in the workspace, and the Workspace directory contains three subdirectories

  • SRC holds the Go source code, which is organized into packages (one for each directory)
  • PKG stores compiled files, such as.a files (static library files)
  • Bin Stores the compiled executable file

We can build source packages using the Go tool and install the generated binaries into the PKG and bin directories. As a practical exercise, the Hello World starter from the previous section is stored this way. In workspace, create directories SRC, PKG, and bin, and save hello. Go in GOPATH/ SRC /hello.

hello.go

go install hello/
Copy the code

We can execute this command in any directory on the system, and the Go tool will look for the source code in the Hello/package, based on the specified GOPATH. If you run go Install in the package path, you can omit the package path. The go install command produces an executable binary, which it then installs into the bin directory of the workspace, in this case $GOPATH/bin/hello.

$GOPATH/bin/hello
$GOPATH

$GOPATH/bin

Your first library

Write a library and have a Hello program call it. In the $github.com/Seekload/stringutil GOPATH/create package under the SRC directory

reverse.go

package stringutil
// Reverse Reverses the argument string left and right in runic units.
func Reverse(s string) string {
	r := []rune(s)
	for i, j := 0.len(r)- 1; i < len(r)/2; i, j = i+1, j- 1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}
Copy the code

The directory is shown as follows:

go build

go build github.com/Seekload/stringutil
Copy the code

You can also execute Go Build directly from the package directory. This command does not produce an output file. To output, you must use the go install command, which places the compiled static files in the PKG directory of your workspace.

stringutil

package main

import (
	"fmt"
	"github.com/Seekload/stringutil"
)

func main(a) {
	fmt.Println(stringutil.Reverse(! "" oG ,daolkeeS"))}Copy the code

Run go install hello/, recompile hello.go, generate executable hello and execute:

hello.go
github.com/Seekload/stringutil
go intall hello/
stringutil
go install github.com/Seekload/stringutil
stringutil.a
pkg/darwin_amd64
darwin_amd64

This section involves the concept of a bag, which may not be clear to some of you. Don’t worry, I’ll show you packages in Go in the next section.


Original article, if need to be reproduced, please indicate the source! Check out “Golang is coming” or go to seekload.net for more great articles.

The public account “Golang is coming” has prepared a mystery learning gift package for you, and the background replies [ebook] to get it!