background

This chapter describes how to configure the local environment to start programming with Go. My machine is a MAC.

In this paper, the structure

  1. Introduces the concept of go’s workspace
  2. Configure the GOPATH environment variable
  3. This section describes the package path name
  4. The practice of creating your first program and package
  5. The test framework

The workspace

The GO tool is designed for open source code maintained in a common code repository. The model sets up the working environment the same way whether you publish code or not.

The Go code must be in the workspace. It is simply a directory containing three subdirectories:

The SRC directory contains the Go source files, which are organized into packages (one for each directory), the PKG directory contains package objects, and the bin directory contains executable commands. The Go tool is used to build source packages and install their generated binaries into the PKG and bin directories.

Bin/hello # Executable command PKG /...... src/ github.com/vir56k/helloCopy the code

Configure the GOPATH environment variable

Start by creating a workspace directory and setting up the appropriate GOPATH. Your workspace can be placed anywhere, and in this document we use $HOME/go. (Another common setting is GOPATH=$HOME.)

  $ mkdir $HOME/work
  $ export GOPATH=$HOME/work
Copy the code

As a convention, add the bin subdirectory of this workspace to your PATH:

$ export PATH=$PATH:$GOPATH/bin
Copy the code

Note that on MAC HOME is the root of your account. Like my machine, I put my workspace in a path with the personal name HOME/go, which is actually /Users/zhangyunfei/go.

Modify the environment variable file:.bash_profile command action: vi ~/.bash_profile add to the end of the file

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
Copy the code

Package path

Packages in the standard library have given short paths, such as “FMT” and “NET/HTTP”. For your own packages, you must choose a base path to ensure that it will not conflict with packages that will be added to the standard library or other extension libraries.

If you put your code into a repository somewhere, use the root directory of that repository as your base path. For example, if you have an account on GitHub, github.com/user, it should be your base path.

For example, I put my package in the SRC github.com/vir56k directory. The name makes it unique, similar to the concept of packages in Java.

Your first program

In src/github.com/user/hello directory Create named hello. Go file, its content is below the go code:

package main

import "fmt"

func main() {
    fmt.Printf("Hello, world.\n")
}
Copy the code

Now you can build and install the program using the Go utility:

$ cd src/github.com/user/hello
$ go install
Copy the code

Now you can run it by typing its full path from the command line:

$ hello
Hello, world.
Copy the code

So we just created a program, which is basically a go file with a main function, and now we’re going to create a package.

Your first library

Let’s write a library and have a Hello program use it.

Again, the first step is to select the package path

$ mkdir src/github.com/user/stringutil
Copy the code

Create a file named reverse.go with the following contents:

// Stringutil contains utility functions for manipulating strings. 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

To compile it, type on the command line:

$ go build
Copy the code

Can. This does not produce an output file. To output, you must use the go install command, which places the package’s objects in the PKG directory of the workspace.

After confirm stringutil package build, modify the original hello. Go file (it is located in $GOPATH/src/github.com/user/hello) to use it:

package main import ( "fmt" "github.com/user/stringutil" ) func main() { fmt.Printf(stringutil.Reverse("! oG ,olleH")) }Copy the code

The Go tool installs whatever it depends on, whether it’s an installation package or a binary. So when we go through

  $ go install github.com/user/hello
Copy the code

When you install the Hello program, the Stringutil package is automatically installed.

Run the new version of this program and you should see a new, reversed message:

$ hello
Hello, Go!
Copy the code

When you’re done, your table of contents should look like this:

Bin/hello # Executable command PKG /...... src/ github.com/vir56k/hello/hello.go github.com/vir56k/stringutil/reverse.goCopy the code

The package name

The first statement in the Go source file must be

The name of the packageCopy the code

The name here is the default name used to import the package. (All files in a package must use the same name.)

The convention for Go is that the package name is the last element of the import path: packages imported as “crypto/rot13” should be named ROT13.

Executable commands must use Package main.

All packages linked into a single binary need not have unique package names, only import paths (their full file names).

test

Go has a lightweight testing framework that consists of the Go test command and the Testing package.

You can write tests by creating a file with a name ending in _test.go that contains a function named TestXXX and signed as func (t * testing.t). The test framework runs each of these functions; The test fails if the function calls a failed function such as t.ror or t.ail.

We can do this by creating files

$GOPATH/src/github.com/user/stringutil/reverse_test.go forCopy the code

Stringutil adds a test that looks like this:

package stringutil import "testing" func TestReverse(t *testing.T) { cases := []struct { in, want string }{ {"Hello, World ", "dlrow olleH"}, {" Hello, world ", "world, olleH"}, {" ", ""},} for _, c: = range cases in {got: = Reverse (c.i n) if got! = c.want { t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want) } } }Copy the code

Then run the test using go test:

$ go test
ok      github.com/user/stringutil 0.165s
Copy the code

END