Go language package and go tools

introduction

Go comes with more than 100 packages, which can provide the foundation for most applications. Go community is a thriving ecological environment that encourages package design, sharing, reuse and improvement. Many packages that have been released can be found in https://godoc.org Found.

The purpose of package management system is to classify the associated characteristics, organize them into units that are easy to understand and modify, and keep them independent from other packages of the program, so as to help design and maintain large-scale programs.

Go package management is similar to Java dependency management, such as maven. The difference is that go package management does not need to specify GAV coordinates, but requires the caller to actively download package files.

The package provides encapsulation capability by controlling whether the name is exported to make it visible outside the package, limiting the visibility of package members, so as to hide the auxiliary functions and types behind the API, and allow the package maintainer to modify the implementation of the package without affecting the code outside the package. Limiting the visibility of variables can also hide variables, so that users can only access and update them by exporting functions. They can retain their own invariants and realize mutually exclusive access in concurrent programs.

import path

Each package is identified by a unique string, which is called the import path. As follows:

import "fmt"

Package declaration

Package specification is required at the beginning of each Go source file. Its main purpose is to use the package as its default identifier when it is introduced by other packages.

For example, each file in the math/rand package starts with package rand, so that when you import the package, you can access its members, such as Rand Int,rand.Float64 et al.

package main

import(
 "fmt"
    "math/rand"
)

func main(){
    fmt.Println(rand.Int())
}

Import declaration

import "fmt"
import "os"

Equivalent to

import {
    "fmt"
    "os"
}

Empty import

If the name of the imported package is not referenced in the file, a compilation error will be caused. But sometimes we have to import a package just to take advantage of its side effect: perform initialization expression evaluation on package level variables and execute its init function. To prevent the "unused import" error, we must use a renamed import with an alternative name, This indicates that the imported content is a blank identifier. Generally, blank identifiers cannot be referenced.

import _ "image/png" //Register PNG decoder

This is called blank import. In most cases, it is used to implement a compile time mechanism to import additional packages using blank references to turn on the optional features of the main program.

Package and its naming

Package names should be as short as possible, readable and unambiguous. For example, instead of naming an auxiliary toolkit util, it is more appropriate to use imageutil or ioutil.

The package name usually uses a unified form. The standard packages bytes, errors and strings use plural numbers to avoid overwriting the pre declared types of the response. The form go/types is used to avoid conflicts with keywords.

Go tool

Go tool combines different kinds of tools into a named set, It is a package manager (similar to apt or rpm). It can query the author of packages, calculate their dependencies, and download them from a remote version control system. It is a build system that can calculate File Dependencies, call compilers, assemblers, and linkers, although it is not complete with the standard UNIX make command.

Common commands are listed below:

The commands are:

        bug         start a bug report
        build       compile packages and dependencies
        clean       remove object files and cached files
        doc         show documentation for package or symbol
        env         print Go environment information
        fix         update packages to use new APIs
        fmt         gofmt (reformat) package sources
        generate    generate Go files by processing source
        get         add dependencies to current module and install them
        install     compile and install packages and dependencies
        list        list packages or modules
        mod         module maintenance
        run         compile and run Go program
        test        test packages
        tool        run specified go tool
        version     print Go version
        vet         report likely mistakes in packages

Organization of workspaces

The only configuration that most users must make is the GOPATH environment variable, which specifies the root directory of the workspace.

There are three subdirectories under GOPATH:

  • The src subdirectory contains source files. Each package is placed in a directory. The name of the directory relative to $GOPATH/src is the import path of the package.

  • The pkg subdirectory is where the build tool stores the compiled package.

  • The bin directory is the directory where executable programs are stored.

The go env command outputs environment variables with valid values and their set values related to the tool chain. It also outputs environment variables without valid values and their default values. As follows:

Package download

If you use the go tool, the import path of the package not only indicates how to find its location in the local space, but also indicates the location where you can use go get to obtain and update it through the Internet.

The go get command can download a single package or use Symbols to download subtrees or warehouses.

The go get command supports several popular code hosting sites, such as GitHub, BitBucket and launchpad, and can send appropriate requests to the version control system.

Package construction

The go build command compiles the packages in each command line parameter. If the package name is main,go build calls the linker to create an executable program in the current directory. The name of the executable program is taken from the last section of the package import path.

Packages can be specified by specifying a directory. You can use an import path or a relative directory name. The directory must be in Or start.

As follows:

cd anywhere
go build gopl.io/njn/jbnj

The above is about the package.

I hope I can help you~

Keywords: Go

Added by idweb on Mon, 20 Dec 2021 00:07:09 +0200