SoFunction
Updated on 2025-03-05

Golang Minimalist Introductory Tutorial (IV): Write the First Project

workspace

Golang's code must be placed in a workspace. A workspace is a directory that contains several subdirectories:

Table of contents. Contains source files, which are organized into packages (one directory, one package)
Table of contents. Contains package objects
Table of contents. Contains executable commands

The package source file is compiled into a package object, and the command source file is compiled into a command executable. The generated package object used to build using the go command is located in the pkg directory, and the generated executable command is located in the bin directory. To develop Golang, you need to set an environment variable GOPATH, which is used to specify the path of the workspace. In addition, the workspace bin directory can also be added to the PATH environment variable.

Package path

Packages are located in the src directory. As long as there is no conflict (for example, conflict with the standard library), we can use any package path. If we have an account name5566 on GitHub and a project hello exists, then we can build such a directory structure in the src directory:

Copy the codeThe code is as follows:

/name5566/hello

The first executable command

Now let's write the first Golang program hello. Assuming that GOPATH has been configured and the package path uses /name5566/hello, create a file in this directory:

Copy the codeThe code is as follows:

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

Execute commands (can be executed anywhere):

Copy the codeThe code is as follows:

go install /name5566/hello

This command can compile the hello command, generate an executable hello command and place it in the bin directory. If the go command has no output, it means that the execution is successful.

The first package object

Now start creating a package object (that is, the library file), first create the package path /name5566/newmath, and create the file in this directory:

Copy the codeThe code is as follows:

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:

Copy the codeThe code is as follows:

go build /name5566/newmath

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

Let's modify it now:

Copy the codeThe code is as follows:

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

Compile and install hello:

Copy the codeThe code is as follows:

go install /name5566/hello

The go command can analyze the dependencies by itself, and the newmath package here will be automatically installed in the pkg directory.

Also note that Golang uses static linking (no package objects are required to run Go programs).

test

Golang comes with a lightweight test framework, mainly including:

test command
Bag

Create a file _test.go to write tests, for example, the test file created for sqrt_test.go:

Copy the codeThe code is as follows:

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

Execute the test using go test:

Copy the codeThe code is as follows:

go test /name5566/newmath

All test functions contained in the test file func TestXxx(t *) will be executed. The test function can be informed of errors through Error, Fail and other related methods.

In addition, you can also write sample code in the test file, for example:

Copy the codeThe code is as follows:

func ExampleHello() {
    ("hello")
    // Output: hello
}

Here, the end of the Example function can be followed by a comment starting with the "Output:" string (called an output comment). When the test is run, the string in the Example function output comment is compared with the function standard output. It should be noted that if there is no output comment, the Example function will not be run (but will be compiled).

Example function naming has this habit:

Copy the codeThe code is as follows:

func Example() { ... }
func ExampleF() { ... }
func ExampleT() { ... }
func ExampleT_M() { ... }

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

Get packages for remote repository

Use go get to automatically get packages for remote repositories, for example:

Copy the codeThe code is as follows:

go get /p//hello