使用Modules搭建项目-go.mod

我第一次接触Go语言的时候使用goland搭建项目遇到引用第三方包或者本地引用的时候总是报错,而不知道为什么?
 
使用GoPath有很多限制,不如需要制定GOPATH路径还需要src之类的。
 
而go.mod又不知道怎么使用,在网上找了好多而不知所以然。
 
实战配置
首先在Golang的官网: https://golang.google.cn/点击下载
 
选在合适的版本,我的是电脑是mac,所以选在第二个。
 
下载后直接双击,一直下一步即可。
 
验证安装是否完成:输入go version
 
修改环境变量(因为我安装了oh-my-zsh),输入vim ~/.zshrc。如果没有按照的可以输入vim ~/.bash_profile
export GOROOT=/usr/local/go
export GOPATH=$HOME/go_learning
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN:$GOROOT/bin

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
 
输入source ~/.zshrc,让配置生效。
之后输入go env查看配置是否正确
 
go mod 有以下命令:
命令
说明
download
download modules to local cache(下载依赖包)
edit
edit go.mod from tools or scripts(编辑go.mod)
graph
print module requirement graph (打印模块依赖图)
verify
initialize new module in current directory(在当前目录初始化mod)
tidy
add missing and remove unused modules(拉取缺少的模块,移除不用的模块)
vendor
make vendored copy of dependencies(将依赖复制到vendor下)
verify
verify dependencies have expected content (验证依赖是否正确)
why
explain why packages or modules are needed(解释为什么需要依赖)
比较常用的是 init,tidy, edit
初始化一个项目
在项目里面go mod init,如你的项目使gotest目录,那就cd gotest 在执行go mod init
 
module gotest

go 1.14
go.mod 提供了module, require、replace和exclude 四个命令
  • module 语句指定包的名字(路径)
  • require 语句指定的依赖项模块
  • replace 语句可以替换依赖项模块
  • exclude 语句可以忽略依赖项模块
 
如引入github中的goinaction包
 
import (
   "log"
   "sync"
   "time"

   "github.com/goinaction/code/chapter7/patterns/work"
)
在go.mod中就会出现
module gotest

go 1.14

require (
   github.com/goinaction/code v0.0.0-20171020164608-49fc99e6affb
)
本地引入
import (
   "log"
   "os"
   "time"

   "gotest/chapter7/patterns/runner"
)
srctest是go.mod中的 module,随后写后面的路径就ok了。
 
 
在来看看goland怎么配置
 
把这些都勾掉,因为我们不使用GOPATH路径了。
 
在Go Modules中配置如下即可
 
 
这样就可以运行了。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ERROR Failed to compile with 48 errors 上午10:53:54 These dependencies were not found: * core-js/modules/es.array.push.js in ./node_modules/.store/@babel+runtime@7.22.6/node_modules/@babel/runtime/helpers/esm/objectSpread2.js, ./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/.store/babel-loader@8.3.0/node_modules/babel-loader/lib!./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/.store/vue-loader@15.10.1/node_modules/vue-loader/lib??vue-loader-options!./src/components/HeaderSearch/index.vue?vue&type=script&lang=js& and 29 others * core-js/modules/es.error.cause.js in ./node_modules/.store/@babel+runtime@7.22.6/node_modules/@babel/runtime/helpers/esm/regeneratorRuntime.js, ./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/.store/babel-loader@8.3.0/node_modules/babel-loader/lib!./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/.store/vue-loader@15.10.1/node_modules/vue-loader/lib??vue-loader-options!./src/layout/components/Navbar.vue?vue&type=script&lang=js& and 5 others * core-js/modules/es.object.proto.js in ./node_modules/.store/@babel+runtime@7.22.6/node_modules/@babel/runtime/helpers/esm/regeneratorRuntime.js * core-js/modules/es.regexp.dot-all.js in ./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/.store/babel-loader@8.3.0/node_modules/babel-loader/lib!./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/.store/vue-loader@15.10.1/node_modules/vue-loader/lib??vue-loader-options!./src/components/ThemePicker/index.vue?vue&type=script&lang=js&, ./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/.store/babel-loader@8.3.0/node_modules/babel-loader/lib!./node_modules/.store/cache-loader@4.1.0/node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/.store/vue-loader@15.10.1/node_modules/vue-loader/lib??vue-loader-options!./src/layout/components/Navbar.vue?vue&type=script&lang=js& and 2 others * core-js/modules/web.url-search-params.delete.js in ./src/utils/request.js * core-js/modules/web.url-search-params.has.js in ./src/utils/request.js * core-js/modules/web.url-search-params.size.js in ./src/utils/request.js * qs in ./src/utils/request.js * svg-baker-runtime/browser-symbol in ./src/icons/svg/user.svg To install them, you can run: npm install --save core-js/modules/es.array.push.js core-js/modules/es.error.cause.js core-js/modules/es.object.proto.js core-js/modules/es.regexp.dot-all.js core-js/modules/web.url-search-params.delete.js core-js/modules/web.url-search-params.has.js core-js/modules/web.url-search-params.size.js qs svg-baker-runtime/browser-symbol怎么解决如何安装
最新发布
07-21

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值