Customize the directory structure, go install and go test of the package

Posted by kvnirvana on Fri, 04 Feb 2022 07:49:21 +0100

For demonstration, we created a simple package named uc, which contains an UpperCase function to convert all letters of a string to UpperCase. Of course, it's not worth creating your own package. The same functions have been included in the strings package, but the same technology can also be applied to more complex packages.

Customize the directory structure of the package

The following structure gives you a good demonstration:

/home/user/goprograms
    ucmain.go   (uc Package main program)
    Makefile (ucmain of makefile)
    ucmain
    src/uc   (contain uc Wrapped go Source code)
        uc.go
        uc_test.go
        Makefile (Wrapped makefile)
        uc.a
        _obj
            uc.a
        _test
            uc.a
    bin      (Contains the final execution file)
            ucmain
    pkg/linux_amd64
            uc.a    (Target file for package)

Put your project in the goprograms directory (you can create an environment variable GOPATH: add export GOPATH = / home / user / goprograms to the. profile and. bashrc files), and your project will be a subdirectory of src. The functions in uc package are in uc Go.

package uc
import "strings"

func UpperCase(str string) string {
    return strings.ToUpper(str)
}

Packages usually come with one or more test files, where we create a uc_test.go file.

package uc
import "testing"

type ucTest struct {
    in, out string
}

var ucTests = []ucTest {
    ucTest{"abc", "ABC"},
    ucTest{"cvo-az", "CVO-AZ"},
    ucTest{"Antwerp", "ANTWERP"},
}

func TestUC(t *testing.T) {
    for _, ut := range ucTests {
        uc := UpperCase(ut.in)
        if uc != ut.out {
            t.Errorf("UpperCase(%s) = %s, must be %s", ut.in, uc,
            ut.out)
        }
    }
}

Compile and install the package locally through the instruction: go install uc, which will A copy to PKG / Linux_ Under AMD64.

In addition, use make to create a package Makefile in src/uc directory with the following contents:

include $(GOROOT)/src/Make.inc

TARG=uc
GOFILES=\
        uc.go\

include $(GOROOT)/src/Make.pkg

The command line under this directory calls: gomake

This creates a_ obj directory and compile the package to generate an archive UC A put it in this directory.

This package can pass the go test.

Create a UC The test file of a is in the directory. When the output is PASS, the test passes.

Note: it is possible that your current user is not qualified to use go install. In this case, select root user su. Make sure that the Go environment variable and the Go source path are also set to Su, which is also applicable to your ordinary users.

Next, we create the main program ucmain go:

package main
import (
    "./uc/uc"
    "fmt"
)

func main() {
    str1 := "USING package uc!"
    fmt.Println(uc.UpperCase(str1))
}

Then enter go install in this directory.

Also copy UC A go to the / home/user/goprograms directory and create a Makefile and write the text:

include $(GOROOT)/src/Make.inc
TARG=ucmain
GOFILES=\
    ucmain.go\

include $(GOROOT)/src/Make.cmd

Execute gomake to compile ucmain Go generate executable ucmain

Run/ ucmain displays: USING PACKAGE UC!.

Local installation package

The local package is in the user directory. Using the given directory structure, the following command is used to install the local package from the source code:

go install /home/user/goprograms/src/uc # Compile and install uc
cd /home/user/goprograms/uc
go install ./uc     # Compile and install uc (same as previous instructions)
cd ..
go install .    # Compile and install ucmain

Install under $GOPATH:

If the package we want to install is used in other Go programs on the system, it must be installed under $GOPATH.

To do so, in profile and Set export GOPATH=/home/user/goprograms in bashrc.

Then execute go install uc to archive the copied package to $GOPATH/pkg/LINUX_AMD64/uc.

Now, the uc package can be referenced in any Go program through import "uc".

System dependent code

It is very rare for programs running on different operating systems to be implemented in different codes: in most cases, languages and standard libraries solve most of the portability problems.

You have a good reason to write platform specific code, such as assembly language. In this case, the following agreement is reasonable:

prog1.go
prog1_linux.go
prog1_darwin.go
prog1_windows.go

  prog1.go defines the common interfaces of different operating systems, and writes the system specific code to prog1os Go.