go Learning (Five): package management

First, set the environment variable

 

 

Second, go modules enable the feature and set the proxy

https://goproxy.io/zh/

supplement:

  GO111MODULE  There are three values: on, off, auto

  • GO111MODULE=off, Go command line support module does not function, look for dependencies manner will use the old version of the kind referred to by their vendor directory or GOPATH mode.
  • GO111MODULE=on, Go command line using the modules, but that will not go GOPATH Find directory.
  • GO111MODULE=autoThe default value, go to the command line will decide whether to enable the function module according to the current directory.

  Enabled the modules, dependencies mounting position at $ GOPATH / pkg directory; go mod provides the following command:

 

 

 

Three, go mod command Management Pack

  • In any path GOPATH that the new project directory, eg: GOPATH directory D: GoCode; project directory D: Project / testProject
  • Into the project directory, create a directory bin, src / main, into the src directory; Note: Here the directory are free.
  • Use the command: go mod init main, this case generates a go.mod  file; NOTE: go mod init command later be able to keep the file name (with the suffix). After go.mod file is created, its contents will be go toolchain full control. go toolchain will be in the various types of command execution, such go get, go build, go mod and other modifications and maintenance go.mod file.
  • Into the main directory, create main.go file, write the code:
package main

import (
    "net/http"
    
    "github.com/labstack/echo"
)

func main() {
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })
    e.Logger.Fatal(e.Start(":1323"))
}
main.go
  • Use the command: go run main.go, the result of normal operation as follows:

  • Principles go module installation package is to first pull the latest release tag, if no tag is pulling the latest commit. go go.sum will automatically generate a file to record the dependency tree. go.mod document reads as follows

 

go.moudles official description: https://github.com/golang/go/wiki/Modules

 

go.mod offers modulerequire, replaceand exclude four command

  • module Statement specifies the package name (path)
  • require Statement specifies dependencies module
  • replace Statements can replace the module dependencies
  • exclude Statement can ignore dependencies module

 

Guess you like

Origin www.cnblogs.com/hsmwlyl/p/11776924.html