The basic concept

  • A package is a collection of functions and data. Functions and data with related features are managed in a unified file/directory
  • Each package can be maintained as a separate unit and made available to other projects

A, the statement

  • All Go source files need to start with a package declaration to indicate which package they are in
  • Package names are in short lowercase letters, often consistent with the name of the directory in which they reside. Multiple Go source files can reside in a package, but the package name must be the same

Working directory structure description:

  • Bin: Used to place published binaries
  • PKG: Library files for publishing
  • SRC: Used to place source code

Import & call

1. Declare two packages with paths gpkgname/pkg01 and gpkgname/pkg02 respectively

gv/src/gpkgname/pkg01/module.go

package pkg01
var Name string = "pgk01"
Copy the code

gv/src/gpkgname/pkg02/module.go

package pkg02
var Name string = "pgk02"
Copy the code

2. Create gv/ SRC /gpkgmain/main.go call gpkgname/pkg01 and gpkgname/pkg02

gv/src/gpkgmain/main.go

package main

import (
	"fmt"
	"gpkgname/pkg01"
	"gpkgname/pkg02"
)

func main(){
	fmt.Println("gpkgmain")
	fmt.Println(pkg01.Name)
	fmt.Println(pkg02.Name)
}
Copy the code

The directory structure looks like this

[root @ happy gv] # tree/Users/wangfei/docker/go/chapter08 / / Users/wangfei/docker/go/chapter08 / └ ─ ─ gv ├ ─ ─ bin └ ─ ─ the SRC ├ ─ ─ Gpkgmain │ └ ─ ─ main. Go └ ─ ─ gpkgname ├ ─ ─ pkg01 │ └ ─ ─ module. Go └ ─ ─ pkg02 └ ─ ─ module. GoCopy the code

3. Add the chapter08/gv directory to the GOPATH environment variable

[root@happy gv]#  GOPATH=/Users/wangfei/docker/go/chapter08/gv
[root@happy gv]# echo $GOPATH
/Users/wangfei/docker/go/chapter08/gv
Copy the code

4. Compile binaries

  • Command :go build gpkgmain
  • Compile path gpkgmain under the package, main package, in the current directory to produce binary named pkgmain
[root@happy gv]# go build gpkgmain [root@happy gv]# ll total 2032 drwxr-xr-x 2 root root 4096 Nov 15 10:36 bin -rwxr-xr-x 1 root root 2068379 Nov 15 10:36 gpkgmain drwxr-xr-x 3 root root 4096 Nov 15 10:37 pkg drwxr-xr-x 4 root root 4096 Nov 15 whosoever SRC directory structure is such a tree/Users/wangfei/docker/go/chapter08 / / Users/wangfei docker/go/chapter08 └ ─ ─ gv ├ ─ ─ gpkgmainCopy the code

5. Run the binary file

[root@happy gv]# ./gpkgmain
gpkgmain
pgk01
pgk02
Copy the code

6. Compile and publish binaries

  • Command :go install gpkgmain
  • Note: Compile and publish the main package in gv/ SRC /gpkgmain, which copies the compiled binary file pkgmain to gv/bin
[root@happy gv]# go install gpkgmain

[root@happy gv]# ls /Users/wangfei/docker/go/chapter08/gv/bin/
gpkgmain

Copy the code

7. Compile and publish library files

  • Command :go install gpkgname/pkg01
  • Compile and publish package under path gpkgname/pkg01, not main package, Copy the compiled library file with the package name to the PKG /GOOS_GOARCH directory (Linux is PKG /linux_amd64 GOOS_GOARCH will change depending on the platform type)
[root@happy gv]# go install gpkgname/pkg01 [root@happy gv]# go install gpkgname/pkg02 [root@happy gv]# tree / Users/wangfei docker/go/chapter08 / gv/Users/wangfei docker/go/chapter08 / gv ├ ─ ─ PKG │ └ ─ ─ linux_amd64 │ └ ─ ─ gpkgname │ ├── │ ├─ │ ├─ │ ├─ │ ├─Copy the code

8. Compile and publish all binary and library files

  • Command :go install./…
  • Description: Compile and publish all binaries and library files in the current path
  • Note: The Go language does not allow cross-importing packages
[root@happy gv]# go install ./...
Copy the code

Three, import form

1. Import absolute paths

  • Look for packages in the GOPATH directory
import "fmt"
import "gpkgname/pkg01"
Copy the code

2. Import relative paths

  • Look in the directory where the current file is located
import "./gpkgname/pkg02"
Copy the code

3. The point of import

  • You can call directly with the member name (omitting the package name) when importing a member from a package at the call point
import . "fmt"

func main(){
	Println("Hello World!")
}
Copy the code

4. Import the alias

  • When importing the same package name in different paths, rename the imported package using an alias to avoid conflicts
package main
import f "fmt"

func main(){
	f.Println("Hello World!")
}
Copy the code

5. Underline import

  • Go does not allow packages to be imported but is not used. In some cases, packages need to be initialized and imported using whitespace as an alias so that initialization functions in packages can be executed
package main
import _ "fmt"
Copy the code

Member visibility and functions

1. Member visibility

  • The Go language uses case to determine access to objects (constants, variables, functions, types, structures, methods, and so on)
  • Capital letter identifies visible outside the package (public) otherwise accessible only inside the package (internal)

2. Main package and main function

  • The main package is used to declare that it tells the compiler to compile the package as a binary executable
  • The main function in the main package is the entry to the program and has no return value and no arguments

3. The init function

  • The init function is used to initialize the package and has no return value and no arguments
  • Init function is automatically called when importing packages (const->var->init)

pkg01/module.go

Package pkg01 import "FMT" // Public var Name string = "pkg01" // private var version string = "v2.0" // init func init(){ fmt.Println("version is :", version) }Copy the code

main.go

package main import ( "fmt" "gpkgname/pkg01" "gpkgname/pkg02" ) func init(){ fmt.Println("main init") } func main(){ Fmt. Println("gpkgmain") fmt.Println(pkg01.Name) // Call pkg02 member Name}Copy the code

5. Go package management

  • Go1.11 􏰀 allows the Go Modules mechanism to manage packages, while retaining GOPATH and vendor mechanisms, and controlling the temporary environment variable GO111MODULE
  • Three values for GO111MODULE
    • With GO111MODULE set to off, the build project always searches the GOPATH and Vendor directories for the target program dependencies
    • GO111MODULE is on and always uses the Gomodules mechanism to search for target program dependencies in the GOPATH/ PKG /mod directory
    • GO111MODULE is auto(default)

GOPATH + vendor mechanism

1.vendor

  • Copy the project dependency packages to the vendor directory under the project, and use the packages in the vendor directory under the project for compilation
  • Solve a problem:
    • You need to use Go Get to download third-party packages
    • Third-party packages cannot guarantee development and compile-time version compatibility once downloaded by Go Get

2. Search order

  • Look in the vendor directory under the current package
  • Look in the parent directory until you reach the GOPATH/ SRC /vendor directory
  • Look in the GOPATH directory
  • Look in the GOROOT directory

3. Third-party packages

  • You can download and install third-party packages and their dependencies using the Go Get tool
  • You need to install a code management tool that matches the third-party package, such as Git and SVN
GOPATH=/Users/wangfei/docker/go/chapter08/gv

go get -x github.com/astaxie/beego
Copy the code

Common parameters:

  • -d: Downloads only dependent packages
  • -u: updates the package and installs it
  • -x: prints the executed commands
  • -v: prints the built package
  • -insecure: The package can be downloaded using HTTP

Third-party package lookup address:

  • godoc.org
  • gowalker.org/

Go modules mechanism

Advantage:

  • Without setting GOPATH, the code can be placed wherever it wants
  • Automatic download dependency management
  • Version control
  • Relative imports are not allowed
  • The replace mechanism

Initialize the module

  • Command: go mod init modname

When importing packages in the current module, run modname+packagename

Third-party packages

  • Use go Mod Tidy, Go Build, go test, and go List to automatically write third-party dependency packages into the go.mod file
  • At the same time, download third-party dependencies to GOPATH/ PKG /mod/cache and generate a build status tracking file go.sum in the current module directory
  • The file records all top-level and indirect dependencies of the current Module, as well as the checksums of those dependencies

Common commands

  • Go Mod Tidy: Tidy dependent modules (add new ones, delete unused ones)
  • Go Mod vendor: Copies dependent modules to the vendor directory of the module
  • Go Build: Compiles the current module
  • go build ./… : Compiles all modules in the current directory
  • Gobuild-mod =vendor: Compile using packages in the vendor directory of the current module
  • Go Mod Download: Downloads only third-party modules
  • Go Mod Grapha: Prints all third party modules
  • Go list -m -json all: Displays information about all modules
  • go mod edit: Modify the go.mod file -require=pakcage@version -replace =old_package@version=new_package@version You can use the -replace function to replace the package with a local package to implement relative import

Standard package

Go 􏰀 provides a large number of standard packages, which can be viewed at golang.google.cn/pkg/

Godoc tools

  • Use the godoc command to launch the Golang website locally for viewing the help manual locally
    • godoc –http=localhost:8888
  • Go list STD: View all standard packages
  • Go Doc Packagename: View the help information about the package
  • Go doc packagename. Element: View the help information about a package member