tags:golang

只需要golang的版本是1.11及之后的,这个模块就内置好了

(1) 配置GoLang的GOROOT

(2) 可以不配置GoLang的GOPATH

(3) 配置mod模块的状态

  1. export GO111MODULE=auto
  2. 可选值有三个:on:开启,off:关闭,auto:自动
  3. 如果是自动模式,文件夹下有go.mod则是开启状态,否则是关闭状态
  4. 如果是开启状态,则GOPATH失效

(4) 配置mod的国内代理

  1. #这是阿里的配置
  2. export GOPROXY=https://mirrors.aliyun.com/goproxy/

打开goland的设置,然后配置如下:

go_mod.jpgfile

  1. $ go mod init bitcoin
  2. go: creating new go.mod: module bitcoin

可以引入 GoLang 的官方包,也可以引入非官方包

  1. $ go mod tidy
  1. $ go help mod
  2. Go mod provides access to operations on modules.
  3. Note that support for modules is built into all the go commands,
  4. not just 'go mod'. For example, day-to-day adding, removing, upgrading,
  5. and downgrading of dependencies should be done using 'go get'.
  6. See 'go help modules' for an overview of module functionality.
  7. Usage:
  8. go mod <command> [arguments]
  9. The commands are:
  10. download download modules to local cache
  11. edit edit go.mod from tools or scripts
  12. graph print module requirement graph
  13. init initialize new module in current directory
  14. tidy add missing and remove unused modules
  15. vendor make vendored copy of dependencies
  16. verify verify dependencies have expected content
  17. why explain why packages or modules are needed
  18. Use "go help mod <command>" for more information about a command.

翻译一下

  1. Go mod提供对模块操作的访问。
  2. 注意,所有的go命令都内置了对模块的支持,而不仅仅是"go mod"。例如,日常应该使用"go get"来完成依赖的添加、删除、升级、降级。
  3. 更多模块功能的概述,请参阅"go help modules",我把它放在附录里了。
  4. 用法:
  5. go mod <命令> [参数]
  6. 命令如下:
  7. download 将模块下载到本地缓存
  8. edit 通过工具或脚本编辑"go.mod"
  9. graph 打印模块需求图
  10. init 当前目录下初始化新的模块,后接参数模块名
  11. tidy 添加需要的依赖和删除未使用的依赖
  12. verdor 把依赖项复制到vendor文件中
  13. verify 验证依赖项是否被修改过
  14. why 解释为什么需要包或模块
  1. $ go mod init
  2. go: cannot determine module path for source directory /Users/shanpengfei/work/go-work-space/src/github.com/shanpengfei7/bitcoin (outside GOPATH, no import comments)

解决方法:init后面应该加上模块的名字

  1. $ go mod init bitcoin
  2. go: creating new go.mod: module bitcoin
  1. $ go mod init
  2. go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'
  3. $ go mod init bitcoin
  4. go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'

解决方法:不要在$GOPATH路径下执行该命令。或者换个路径,或者把环境变量中的$GOPATH置空

看一下官方介绍 go module 的原文:

  1. A module is a collection of related Go packages.
  2. Modules are the unit of source code interchange and versioning.
  3. The go command has direct support for working with modules,
  4. including recording and resolving dependencies on other modules.
  5. Modules replace the old GOPATH-based approach to specifying
  6. which source files are used in a given build.
  7. Preliminary module support
  8. Go 1.11 includes preliminary support for Go modules,
  9. including a new module-aware 'go get' command.
  10. We intend to keep revising this support, while preserving compatibility,
  11. until it can be declared official (no longer preliminary),
  12. and then at a later point we may remove support for work
  13. in GOPATH and the old 'go get' command.
  14. The quickest way to take advantage of the new Go 1.11 module support
  15. is to check out your repository into a directory outside GOPATH/src,
  16. create a go.mod file (described in the next section) there, and run
  17. go commands from within that file tree.
  18. For more fine-grained control, the module support in Go 1.11 respects
  19. a temporary environment variable, GO111MODULE, which can be set to one
  20. of three string values: off, on, or auto (the default).
  21. If GO111MODULE=off, then the go command never uses the
  22. new module support. Instead it looks in vendor directories and GOPATH
  23. to find dependencies; we now refer to this as "GOPATH mode."
  24. If GO111MODULE=on, then the go command requires the use of modules,
  25. never consulting GOPATH. We refer to this as the command being
  26. module-aware or running in "module-aware mode".
  27. If GO111MODULE=auto or is unset, then the go command enables or
  28. disables module support based on the current directory.
  29. Module support is enabled only when the current directory is outside
  30. GOPATH/src and itself contains a go.mod file or is below a directory
  31. containing a go.mod file.
  32. In module-aware mode, GOPATH no longer defines the meaning of imports
  33. during a build, but it still stores downloaded dependencies (in GOPATH/pkg/mod)
  34. and installed commands (in GOPATH/bin, unless GOBIN is set).
  35. Defining a module
  36. A module is defined by a tree of Go source files with a go.mod file
  37. in the tree's root directory. The directory containing the go.mod file
  38. is called the module root. Typically the module root will also correspond
  39. to a source code repository root (but in general it need not).
  40. The module is the set of all Go packages in the module root and its
  41. subdirectories, but excluding subtrees with their own go.mod files.
  42. The "module path" is the import path prefix corresponding to the module root.
  43. The go.mod file defines the module path and lists the specific versions
  44. of other modules that should be used when resolving imports during a build,
  45. by giving their module paths and versions.
  46. For example, this go.mod declares that the directory containing it is the root
  47. of the module with path example.com/m, and it also declares that the module
  48. depends on specific versions of golang.org/x/text and gopkg.in/yaml.v2:
  49. module example.com/m
  50. require (
  51. golang.org/x/text v0.3.0
  52. gopkg.in/yaml.v2 v2.1.0
  53. )
  54. The go.mod file can also specify replacements and excluded versions
  55. that only apply when building the module directly; they are ignored
  56. when the module is incorporated into a larger build.
  57. For more about the go.mod file, see 'go help go.mod'.
  58. To start a new module, simply create a go.mod file in the root of the
  59. module's directory tree, containing only a module statement.
  60. The 'go mod init' command can be used to do this:
  61. go mod init example.com/m
  62. In a project already using an existing dependency management tool like
  63. godep, glide, or dep, 'go mod init' will also add require statements
  64. matching the existing configuration.
  65. Once the go.mod file exists, no additional steps are required:
  66. go commands like 'go build', 'go test', or even 'go list' will automatically
  67. add new dependencies as needed to satisfy imports.
  68. The main module and the build list
  69. The "main module" is the module containing the directory where the go command
  70. is run. The go command finds the module root by looking for a go.mod in the
  71. current directory, or else the current directory's parent directory,
  72. or else the parent's parent directory, and so on.
  73. The main module's go.mod file defines the precise set of packages available
  74. for use by the go command, through require, replace, and exclude statements.
  75. Dependency modules, found by following require statements, also contribute
  76. to the definition of that set of packages, but only through their go.mod
  77. files' require statements: any replace and exclude statements in dependency
  78. modules are ignored. The replace and exclude statements therefore allow the
  79. main module complete control over its own build, without also being subject
  80. to complete control by dependencies.
  81. The set of modules providing packages to builds is called the "build list".
  82. The build list initially contains only the main module. Then the go command
  83. adds to the list the exact module versions required by modules already
  84. on the list, recursively, until there is nothing left to add to the list.
  85. If multiple versions of a particular module are added to the list,
  86. then at the end only the latest version (according to semantic version
  87. ordering) is kept for use in the build.
  88. The 'go list' command provides information about the main module
  89. and the build list. For example:
  90. go list -m # print path of main module
  91. go list -m -f={{.Dir}} # print root directory of main module
  92. go list -m all # print build list
  93. Maintaining module requirements
  94. The go.mod file is meant to be readable and editable by both
  95. programmers and tools. The go command itself automatically updates the go.mod file
  96. to maintain a standard formatting and the accuracy of require statements.
  97. Any go command that finds an unfamiliar import will look up the module
  98. containing that import and add the latest version of that module
  99. to go.mod automatically. In most cases, therefore, it suffices to
  100. add an import to source code and run 'go build', 'go test', or even 'go list':
  101. as part of analyzing the package, the go command will discover
  102. and resolve the import and update the go.mod file.
  103. Any go command can determine that a module requirement is
  104. missing and must be added, even when considering only a single
  105. package from the module. On the other hand, determining that a module requirement
  106. is no longer necessary and can be deleted requires a full view of
  107. all packages in the module, across all possible build configurations
  108. (architectures, operating systems, build tags, and so on).
  109. The 'go mod tidy' command builds that view and then
  110. adds any missing module requirements and removes unnecessary ones.
  111. As part of maintaining the require statements in go.mod, the go command
  112. tracks which ones provide packages imported directly by the current module
  113. and which ones provide packages only used indirectly by other module
  114. dependencies. Requirements needed only for indirect uses are marked with a
  115. "// indirect" comment in the go.mod file. Indirect requirements are
  116. automatically removed from the go.mod file once they are implied by other
  117. direct requirements. Indirect requirements only arise when using modules
  118. that fail to state some of their own dependencies or when explicitly
  119. upgrading a module's dependencies ahead of its own stated requirements.
  120. Because of this automatic maintenance, the information in go.mod is an
  121. up-to-date, readable description of the build.
  122. The 'go get' command updates go.mod to change the module versions used in a
  123. build. An upgrade of one module may imply upgrading others, and similarly a
  124. downgrade of one module may imply downgrading others. The 'go get' command
  125. makes these implied changes as well. If go.mod is edited directly, commands
  126. like 'go build' or 'go list' will assume that an upgrade is intended and
  127. automatically make any implied upgrades and update go.mod to reflect them.
  128. The 'go mod' command provides other functionality for use in maintaining
  129. and understanding modules and go.mod files. See 'go help mod'.
  130. The -mod build flag provides additional control over updating and use of go.mod.
  131. If invoked with -mod=readonly, the go command is disallowed from the implicit
  132. automatic updating of go.mod described above. Instead, it fails when any changes
  133. to go.mod are needed. This setting is most useful to check that go.mod does
  134. not need updates, such as in a continuous integration and testing system.
  135. The "go get" command remains permitted to update go.mod even with -mod=readonly,
  136. and the "go mod" commands do not take the -mod flag (or any other build flags).
  137. If invoked with -mod=vendor, the go command assumes that the vendor
  138. directory holds the correct copies of dependencies and ignores
  139. the dependency descriptions in go.mod.
  140. Pseudo-versions
  141. The go.mod file and the go command more generally use semantic versions as
  142. the standard form for describing module versions, so that versions can be
  143. compared to determine which should be considered earlier or later than another.
  144. A module version like v1.2.3 is introduced by tagging a revision in the
  145. underlying source repository. Untagged revisions can be referred to
  146. using a "pseudo-version" like v0.0.0-yyyymmddhhmmss-abcdefabcdef,
  147. where the time is the commit time in UTC and the final suffix is the prefix
  148. of the commit hash. The time portion ensures that two pseudo-versions can
  149. be compared to determine which happened later, the commit hash identifes
  150. the underlying commit, and the prefix (v0.0.0- in this example) is derived from
  151. the most recent tagged version in the commit graph before this commit.
  152. There are three pseudo-version forms:
  153. vX.0.0-yyyymmddhhmmss-abcdefabcdef is used when there is no earlier
  154. versioned commit with an appropriate major version before the target commit.
  155. (This was originally the only form, so some older go.mod files use this form
  156. even for commits that do follow tags.)
  157. vX.Y.Z-pre.0.yyyymmddhhmmss-abcdefabcdef is used when the most
  158. recent versioned commit before the target commit is vX.Y.Z-pre.
  159. vX.Y.(Z+1)-0.yyyymmddhhmmss-abcdefabcdef is used when the most
  160. recent versioned commit before the target commit is vX.Y.Z.
  161. Pseudo-versions never need to be typed by hand: the go command will accept
  162. the plain commit hash and translate it into a pseudo-version (or a tagged
  163. version if available) automatically. This conversion is an example of a
  164. module query.
  165. Module queries
  166. The go command accepts a "module query" in place of a module version
  167. both on the command line and in the main module's go.mod file.
  168. (After evaluating a query found in the main module's go.mod file,
  169. the go command updates the file to replace the query with its result.)
  170. A fully-specified semantic version, such as "v1.2.3",
  171. evaluates to that specific version.
  172. A semantic version prefix, such as "v1" or "v1.2",
  173. evaluates to the latest available tagged version with that prefix.
  174. A semantic version comparison, such as "<v1.2.3" or ">=v1.5.6",
  175. evaluates to the available tagged version nearest to the comparison target
  176. (the latest version for < and <=, the earliest version for > and >=).
  177. The string "latest" matches the latest available tagged version,
  178. or else the underlying source repository's latest untagged revision.
  179. A revision identifier for the underlying source repository,
  180. such as a commit hash prefix, revision tag, or branch name,
  181. selects that specific code revision. If the revision is
  182. also tagged with a semantic version, the query evaluates to
  183. that semantic version. Otherwise the query evaluates to a
  184. pseudo-version for the commit.
  185. All queries prefer release versions to pre-release versions.
  186. For example, "<v1.2.3" will prefer to return "v1.2.2"
  187. instead of "v1.2.3-pre1", even though "v1.2.3-pre1" is nearer
  188. to the comparison target.
  189. Module versions disallowed by exclude statements in the
  190. main module's go.mod are considered unavailable and cannot
  191. be returned by queries.
  192. For example, these commands are all valid:
  193. go get github.com/gorilla/mux@latest # same (@latest is default for 'go get')
  194. go get github.com/gorilla/mux@v1.6.2 # records v1.6.2
  195. go get github.com/gorilla/mux@e3702bed2 # records v1.6.2
  196. go get github.com/gorilla/mux@c856192 # records v0.0.0-20180517173623-c85619274f5d
  197. go get github.com/gorilla/mux@master # records current meaning of master
  198. Module compatibility and semantic versioning
  199. The go command requires that modules use semantic versions and expects that
  200. the versions accurately describe compatibility: it assumes that v1.5.4 is a
  201. backwards-compatible replacement for v1.5.3, v1.4.0, and even v1.0.0.
  202. More generally the go command expects that packages follow the
  203. "import compatibility rule", which says:
  204. "If an old package and a new package have the same import path,
  205. the new package must be backwards compatible with the old package."
  206. Because the go command assumes the import compatibility rule,
  207. a module definition can only set the minimum required version of one
  208. of its dependencies: it cannot set a maximum or exclude selected versions.
  209. Still, the import compatibility rule is not a guarantee: it may be that
  210. v1.5.4 is buggy and not a backwards-compatible replacement for v1.5.3.
  211. Because of this, the go command never updates from an older version
  212. to a newer version of a module unasked.
  213. In semantic versioning, changing the major version number indicates a lack
  214. of backwards compatibility with earlier versions. To preserve import
  215. compatibility, the go command requires that modules with major version v2
  216. or later use a module path with that major version as the final element.
  217. For example, version v2.0.0 of example.com/m must instead use module path
  218. example.com/m/v2, and packages in that module would use that path as
  219. their import path prefix, as in example.com/m/v2/sub/pkg. Including the
  220. major version number in the module path and import paths in this way is
  221. called "semantic import versioning". Pseudo-versions for modules with major
  222. version v2 and later begin with that major version instead of v0, as in
  223. v2.0.0-20180326061214-4fc5987536ef.
  224. As a special case, module paths beginning with gopkg.in/ continue to use the
  225. conventions established on that system: the major version is always present,
  226. and it is preceded by a dot instead of a slash: gopkg.in/yaml.v1
  227. and gopkg.in/yaml.v2, not gopkg.in/yaml and gopkg.in/yaml/v2.
  228. The go command treats modules with different module paths as unrelated:
  229. it makes no connection between example.com/m and example.com/m/v2.
  230. Modules with different major versions can be used together in a build
  231. and are kept separate by the fact that their packages use different
  232. import paths.
  233. In semantic versioning, major version v0 is for initial development,
  234. indicating no expectations of stability or backwards compatibility.
  235. Major version v0 does not appear in the module path, because those
  236. versions are preparation for v1.0.0, and v1 does not appear in the
  237. module path either.
  238. Code written before the semantic import versioning convention
  239. was introduced may use major versions v2 and later to describe
  240. the same set of unversioned import paths as used in v0 and v1.
  241. To accommodate such code, if a source code repository has a
  242. v2.0.0 or later tag for a file tree with no go.mod, the version is
  243. considered to be part of the v1 module's available versions
  244. and is given an +incompatible suffix when converted to a module
  245. version, as in v2.0.0+incompatible. The +incompatible tag is also
  246. applied to pseudo-versions derived from such versions, as in
  247. v2.0.1-0.yyyymmddhhmmss-abcdefabcdef+incompatible.
  248. In general, having a dependency in the build list (as reported by 'go list -m all')
  249. on a v0 version, pre-release version, pseudo-version, or +incompatible version
  250. is an indication that problems are more likely when upgrading that
  251. dependency, since there is no expectation of compatibility for those.
  252. See https://research.swtch.com/vgo-import for more information about
  253. semantic import versioning, and see https://semver.org/ for more about
  254. semantic versioning.
  255. Module code layout
  256. For now, see https://research.swtch.com/vgo-module for information
  257. about how source code in version control systems is mapped to
  258. module file trees.
  259. Module downloading and verification
  260. The go command maintains, in the main module's root directory alongside
  261. go.mod, a file named go.sum containing the expected cryptographic checksums
  262. of the content of specific module versions. Each time a dependency is
  263. used, its checksum is added to go.sum if missing or else required to match
  264. the existing entry in go.sum.
  265. The go command maintains a cache of downloaded packages and computes
  266. and records the cryptographic checksum of each package at download time.
  267. In normal operation, the go command checks these pre-computed checksums
  268. against the main module's go.sum file, instead of recomputing them on
  269. each command invocation. The 'go mod verify' command checks that
  270. the cached copies of module downloads still match both their recorded
  271. checksums and the entries in go.sum.
  272. The go command can fetch modules from a proxy instead of connecting
  273. to source control systems directly, according to the setting of the GOPROXY
  274. environment variable.
  275. See 'go help goproxy' for details about the proxy and also the format of
  276. the cached downloaded packages.
  277. Modules and vendoring
  278. When using modules, the go command completely ignores vendor directories.
  279. By default, the go command satisfies dependencies by downloading modules
  280. from their sources and using those downloaded copies (after verification,
  281. as described in the previous section). To allow interoperation with older
  282. versions of Go, or to ensure that all files used for a build are stored
  283. together in a single file tree, 'go mod vendor' creates a directory named
  284. vendor in the root directory of the main module and stores there all the
  285. packages from dependency modules that are needed to support builds and
  286. tests of packages in the main module.
  287. To build using the main module's top-level vendor directory to satisfy
  288. dependencies (disabling use of the usual network sources and local
  289. caches), use 'go build -mod=vendor'. Note that only the main module's
  290. top-level vendor directory is used; vendor directories in other locations
  291. are still ignored.

欢迎阅读单鹏飞的学习笔记

版权声明:本文为shanpengfei原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/shanpengfei/p/11738093.html