This is the 9th day of my participation in the August Wen Challenge.More challenges in August

In The Java language, there is the concept of package. In Golang, there is the concept of package. In Go, the syntax element package is used to organize source code, and all syntax visibility is defined at the package level.

Go’s source code reuse is based on packages. Packages are completed through package, import, and GOPATH operations.

The main package

Go’s main() package is called main. If the main package wants to reference other code, you need to import it!

package

The SRC directory organizes and holds the Go source files as code packages. Each code package corresponds to a folder in the SRC directory. Each subdirectory is a code package.

The code package name and file directory name are not required to be the same. For example, the file directory is called Hello, but the code package name can be declared as “main”, but the source file in the same directory in the first line of the declaration of the package, must be the same!

Add a package definition to the first line of all. Go files in the same directory to mark the package to which the file belongs.

packageThe package nameCopy the code

The package needs to meet:

  • Sibling files in a directory belong to a package. That is, all files under the same package have the same package name.
  • Files under the same packagepackageYou are advised to set this parameter to the directory name, but it may not be the directory name. That is, the package name can be different from its directory name.
  • The package named main is the entry package of the application. Other packages cannot be used.

The files under the same package belong to the same project file, and can be directly used without import package

Packages can have nested definitions that correspond to nested directories, but the package name should be the same as the directory in which it resides, for example:

// file: /utils/common.go
package utils
// A function that can be exported
func Md5(str string) string {
	data := []byte(str)
	rest := fmt.Sprintf("%x", md5.Sum(data))

	return rest
}
// A function that cannot be exported
func md5V2(str string) string {
	h := md5.New()
	h.Write([]byte(str))
	return hex.EncodeToString(h.Sum(nil))}Copy the code

In a package, you can determine whether an identifier can be exported by uppercase. Only a capital letter can be exported as a public resource.

import

To reference other packages, you can use the import keyword, either individually or in batches.

A: Usually import

// Single import
import "package"
// Batch import
import (
  "package1"
  "package2"
  )
Copy the code

B: Click operation We sometimes see the following way to import packages

import(."fmt"
) 
Copy the code

The implication of this dot operation is that after the package is imported, when you call the function of the package, you can omit the prefix of the package name, that is, before you call

Println(“hello world”) can be omitted as Println(“hello world”)

C: Alias

Alias operation as the name implies we can name the package with another name that we can use easily to remember. When importing, you can define an alias for the package.

import (
  p1 "package1"
  p2 "package2"
  )
// When used: alias operation, prefix becomes our prefix when calling package function
p1.Method()
Copy the code

D: _ Operations If you only need to perform initialization operations when importing packages, you do not need to use other functions, constants, and other resources in the package. You can import packages anonymously when importing them.

This operator is often confusing to many people. Take a look at the following import:

import (
   "database/sql"
   _ "github.com/ziutek/mymysql/godrv"
 ) 
Copy the code

The _ operation actually imports the package, and instead of using the package’s function directly, calls the package’s init function. That is, using underscores as aliases for packages simply executes init().

The path name of the imported package can be a relative path or an absolute path. The absolute path (starting from the project root directory) is recommended.

GOPATH environment variable

Import imports the package by retrieving SRC /package from the GO installation directory (where the GOROOT environment variable is set) and from the directory where the GOPATH environment variable is set. If not, the import fails. GOROOT, which is where the packages built into GO are located. GOPATH is the location of the package that we define ourselves.

When developing a Go project, debug or build, we need to set GOPATH to point to our project directory, where packages from the SRC directory can be imported.

Init () package initialization

Init () and main() are reserved functions in the GO language. We can define init() in the source code. This function is executed when the package is imported. For example, if the package is imported in main and init() is in the package, the code in init() is executed before the main() function is executed to initialize the specific data required by the package. For example: package source:

src/userPackage/tool.go

package userPackage
import "fmt"
func init(a) {
  fmt.Println("tool init")}Copy the code

Main function source code:

src/main.go

package main
import (
  "userPackage"
  )
func main(a) {
  fmt.Println("main run")
  / / use userPackage
  userPackage.SomeFunc()
}
Copy the code

During execution, “tool init” is printed, followed by “main run”.

Let’s take a closer look at the init() and main() functions. The differences in GO are as follows:

  • Similarities:

Two functions cannot be defined with any parameters or return values. This function can only be called automatically by the GO program and cannot be referenced.

  • Difference:

Init can be applied to any package, and multiple can be defined repeatedly. The main function can only be used in the main package, and only one can be defined.

The order in which the two functions are executed:

The go file in the main package is always executed by default.

Init () calls to the same Go file are ordered from top to bottom.

For different files in the same package, sort the file names “from smallest to largest” by string, and then call the init() function in each file in that order.

For packages that do not depend on each other, the init() function in the main package is called in the order of the imports in the main package.

If the package has dependencies, the last dependent is initialized first, for example, if the import order is main — > A — > B — > C, then the initialization order is C — > B — > A — > main, and the corresponding init method is executed once. The main package is always initialized last because it always depends on other packages

Avoid circular imports, such as A – > B – > C – > A.

A package is imported by multiple other packages, but can only be initialized once

Managing external packages

Go allows you to import code from different code bases. For external packages to import, you can use the go get command to remove them and place them in the directory corresponding to GOPATH.

For example, if we want to connect to mysql database through go, we need to download mysql data package first, open terminal and enter the following command:

localhost:~ $ go get github.com/go-sql-driver/mysql
Copy the code

Once installed, you can see the corresponding package directory in the gopath directory SRC.

In other words, with go, it doesn’t really matter whether your code is internal or external, it’s all in GOPATH, and the path of any import package starts from GOPATH; The only difference is that the internal dependency package is written by the developers themselves, and the external dependency package is go get down.

More advanced dependency management go Mod

Higher versions of Go advocate package dependency management using the Go mod, which can be understood as Maven in JAVA.

Open the Go module

Versions 1.11 and 1.12

Add the following two Settings to your system’s environment variables

GO111MODULE=on
GOPROXY=https://goproxy.io
Copy the code

After version 1.13

Note that this method does not overwrite the previous configuration, it is a bit of a trap, you need to delete the system environment variables before setting

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct
Copy the code

GoLand enables the Go mod

Mod Basic Operations

Initialize the MODULE

goMod init testCopy the code

We will notice that a go.mod file will appear in the project root directory. Note that the go.mod file only identifies the project name and the version of Go, which is normal because it is only initialized.

Detection of depend on
go mod tidy
Copy the code

Tidy will detect all imported dependencies in the folder directory and write to the go.mod file. You will notice changes to the go.mod file after writing.

Such as:

module test

go 1.13

require (
    github.com/gin-contrib/sessions v0. 01.
    github.com/gin-contrib/sse v01.. 0 // indirect
    github.com/gin-gonic/gin v14.. 0
    github.com/go-redis/redis v615.6.+incompatible
    github.com/go-sql-driver/mysql v14.1.
    github.com/golang/protobuf v13.2. // indirect
    github.com/jinzhu/gorm v19.11.
    github.com/json-iterator/go v11.7. // indirect
    github.com/kr/pretty v01.. 0 // indirect
    github.com/mattn/go-isatty v0. 010. // indirect
    github.com/sirupsen/logrus v12.. 0
    github.com/ugorji/go v11.7. // indirect
    golang.org/x/sys v0. 0. 0- 20191025021431.- 6c3a3bfe00ae // indirect
    gopkg.in/yaml.v2 v22.4.
)
Copy the code

Dependencies are still not downloaded at this point

Download the dependent

Instead of using Go Get, we need to download the dependencies locally

go mod download
Copy the code

If you don’t set GOPROXY as a domestic mirror, this step will definitely get stuck

At this point, all the dependencies will be downloaded to GOPATH, and the go.sum file will be generated in the root directory, which is the detailed dependencies of the dependencies. However, as we said at the beginning, our project is not in GOPATH, so it is useless to download the packages to GOPATH, so we can not find these packages

Import dependence
go mod vendor
Copy the code

Running this command will transfer the dependencies just downloaded to GOPATH to the vendor(Automatic New) folder in the root directory of the project

This is where we can use these dependencies.

If we use goland tool for development, golAND will turn this function off by default, so we need to turn it on manually (we don’t rule out whether it will be turned on by default in future update).

Common commands:

goMod init # initializego.mod
goMod Tidy # Update dependency filesgoMod Download # Download dependent filesgoMod vendor # transfers dependencies to the local vendor filegoMod Edit # Manually modify dependency filesgoMod graph # Prints dependency graphsgoMod verify # verify dependenciesCopy the code