install package vif包_Go Lang 开发笔记《Golang的包管理》

a93da65f876a38efa824c543564df88e.png
Go Lang 1.13
MacOS Mojave 10.14.6

环境变量

GOROOT

Go Lang 安装目录

GOPATH

Go 命令环境依赖,其中主要目录:

  • bin 可执行相关
  • pkg 编译包相关
  • src 源码包相关

GO111MODULE

默认情况下,GO111MODULE=auto 自动模式

  • auto 自动模式下,$GOPATH/src与go.mod 下 require的包选中选择性使用
  • on 开启模式,1.12后,都会使用go.mod 里 require的包
  • off 关闭模式

步骤1:初始化环境

1.创建项目

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello, world!")
}

2.初始化模块

# go mod init hello

初始化名为hello的模块,并且生成go.modgo.sum记录当前依赖包名及版本信息,由程序自动维护

手动改变 go.mod后需要手动清空go.sum再重新生成:

➜ # go mod tidy      
hello2 imports
	hello/utils: malformed module path "hello/utils": missing dot in first path element 

3.引入包规则为模块名+路径格式,比如:

import (
"hello/utils"

"github.com/astaxie/beego"
)
  • 引入hello模块,在utils目录下的包
  • 引入github.com模块,文件在astaxie/beego目录下的包
注意:相同路径下仅能声明一个包

步骤2:配置VSCode

4.安装 VSCode-Go 插件, 添加一下配置:

"go.useLanguageServer": true,
"[go]": {
    "editor.snippetSuggestions": "none",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true,
    }
},
"gopls": {
    "usePlaceholders": true, // add parameter placeholders when completing a function

    // Experimental settings
    "completeUnimported": true, // autocomplete unimported packages
    "deepCompletion": true,     // enable deep completion
}

更多配置说明

图书

zetachow晓代码:拜拜了,GOPATH君!新版本Golang的包管理入门教程​zhuanlan.zhihu.com
a29accf0acbba67cc66b303cbf14c002.png
https://github.com/golang/tools/blob/master/gopls/doc/vscode.md​github.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值