golang package management

  • Automatically download all dependencies
    • go get -d -v ./...
      • -d Just download the code package, do not perform the installation command
      • -vPrint detailed log and debug log. This flag will add here each downloaded package are printed;
      • ./...This represents the path, on behalf of all the files in the current directory.

modules

  • By go.mod documentation package dependencies, go.sum files from malicious modification

  • Projects do not have to organize under the GOPATH path

    • import source code package is no longer organized under the src directory, but under pkg / mod directory
    • Behind the package name in that directory will have @<tag_num>, so go modyou can distinguish between different versions
      • E.g$GOPATH\pkg\mod\github.com\go-sql-driver\[email protected]
      • The version number is the repo tag
      • Did not play for the tag, go.mod format is pseudo-version. Its meaning isv0.0.0-yyyymmddhhmmss-abcdefabcdef
  • Migrating from govendor to gomod just go mod init 模块名then go run/buildonce

  • By replace to redirect packet address

    • Google packages under the domain can be redirected to GitHub
  • Environment variable GO111MODULEis set on the mandatory use mod mode, the opening is provided on the outer path auto GOPATH

  • Module name can be taken, but for more compatibility, should follow github.com/name/repothe format

Common Commands

go mod init <模块名> # 初始化模块
go mod download # 下载依赖包
go mod tidy # 移除未用到的包,下载缺失的包
go mod verity # 验证包依赖是否正确
go mod vendor # 生成vendor目录

reference

Modules-GitHub
golang Package Penalty for the Module Introduction

vendor

  • vendor.json files in the vendor directory record of all dependent information
    • When uploading the project to GitHub, you can only upload vendor.json file, others can, by govendor sync rely command to reinstall the file according to the vendor folder
# 安装govendor
go get -u -v github.com/kardianos/govendor

# 常用命令
govendor init  # 初始化vendor目录
govendor list  # 列出当前所有包依赖
govendor add package_path # 添加依赖包
govendor add +e # 添加所有为什么GOPATH路径下的依赖包
govendor fetch packge_path #添加远程依赖包
govendor update packge_path #更新依赖包
govendor remove packge_path #删除依赖包
govendor sync #根据vendor.json安装依赖包

Here Insert Picture Description

reference

https://blog.csdn.net/dupeng0811/article/details/89877712

import

  • When we import a third-party package, the compiler to find precompiled package object (under pkg directory), if no package object, you'll go to the source directory to find the corresponding source code to compile
  • When we other items introduced included internala dependent package, Go language will complain at compile time
    • This error does not exist only in the internal package introduced in the current project tree will occur, if the introduction of internal packages of the project in the same project and this error will not occur
      Here Insert Picture Description

      Here Insert Picture Description

init

  • init function will be executed before the main function
  • Need to use _null object identifiers to import a packet, is to perform the function of the bag init
  • We should not have done heavy initialization logic in the init, but to do some simple, lightweight precondition judgment

[Pictures of foreign chains dump fails, the source station may have a security chain mechanism, it is recommended to save the picture down directly upload (img-CPiOTHbD-1570015024097) (C: \ Users \ 35135 \ AppData \ Roaming \ Typora \ typora-user-images \ 1569987665508.png)]

Published 161 original articles · won praise 19 · views 50000 +

Guess you like

Origin blog.csdn.net/winter_wu_1998/article/details/101746005