Golang minimalism tutorial 4: write your first project

  • 2020-05-05 11:23:05
  • OfStack

workspace

The code for Golang must be placed in an workspace. An workspace is a directory that contains several subdirectories:

1. src directory. Contains the source files, which are organized into packages (one directory one package)
2. pkg directory. Contains the package object (package objects)
3. bin directory. Contains the executable command

The package source file (package source) is compiled as a package object (package object), and the command source file (command source) is compiled as an executable command (command executable). The package objects generated by building with the go command are in the pkg directory, and the generated executable commands are in the bin directory. Developing Golang requires setting an environment variable GOPATH, which specifies the workspace path, or adding workspace's bin directory to the PATH environment variable.

package path

The package is in the src directory, and we can use any package path as long as there is no conflict (for example, with the standard library). If we had an account number name5566 at GitHub and a project hello, we could build this directory structure under src:


github.com/name5566/hello

first executable command

Now let's write our first Golang program, hello. Assume that has been configured GOPATH and package path using github. com/name5566 / hello, created under the directory file hello. go:


package main
 
import "fmt"
 
func main() {
    fmt.Printf("Hello, world.\n")
}

Execute command (anywhere) :


go install github.com/name5566/hello

This command compiles the hello command, generates an executable hello, and places it in the bin directory. If there is no output from the go command, the execution is successful.

first package object

Now create a package objects (that is, the library files), first establish package path github. com/name5566 / newmath, created under the directory file sqrt. go:


package newmath
 
func Sqrt(x float64) float64 {
    z := 1.0
    for i := 0; i < 1000; i++ {
        z -= (z*z - x) / (2 * z)
    }
    return z
}

We can test compile this package:


go build github.com/name5566/newmath

This command does not have any output files, so if you need to generate package objects in the pkg directory, use go install.

Now let's modify hello.go:


package main
 
import (
    "fmt"
    "github.com/name5566/newmath"
)
 
func main() {
    fmt.Printf("Hello, world. Sqrt(2) = %v\n", newmath.Sqrt(2))
}

Compile and install hello:


go install github.com/name5566/hello

The go command analyzes dependencies on its own, and the newmath package is automatically installed in the pkg directory.

Also note that Golang USES static linking (running Go programs does not require package objects).

test

Golang comes with a lightweight testing framework that includes:

1.go test command
2.

testing package

Create the file _test.go to write the test. For example, the test file created for sqrt.go is sqrt_test.go:


package newmath
 
import "testing"
 
func TestSqrt(t *testing.T) {
    const in, out = 4, 2
    if x := Sqrt(in); x != out {
        t.Errorf("Sqrt(%v) = %v, want %v", in, x, out)
    }
}

Perform tests using go test:


go test github.com/name5566/newmath

All test functions func TestXxx(t * testing.T) contained in the test file are executed. The test function can tell the error by Error, Fail and other relevant methods.

In addition, you can write sample code in the test file, such as


func ExampleHello() {
    fmt.Println("hello")
    // Output: hello
}

Here the Example function ends with a comment that starts with the string "Output:" (called the output comment), and the test run compares the string in the Example function output comment to the standard output of the function. Note that the Example function will not be run (but will be compiled) without the output comments.

The Example function names have this habit:


package main
 
import "fmt"
 
func main() {
    fmt.Printf("Hello, world.\n")
}
0

Here F is the function name, T is the type name, and T_M is the method M on type T.

gets the package

for the remote repository

go get is used to automatically obtain packages for remote repositories, e.g.


go get code.google.com/p/go.example/hello


Related articles: