博客
关于我
初学Golang的笔记
阅读量:531 次
发布时间:2019-03-08

本文共 1701 字,大约阅读时间需要 5 分钟。

Golang 模块指南

模块的创建与初始化

一个 Go 模块是一个 Go 包的集合,用于发布和分发。通常情况下,一个 Go 仓库只包含一个模块,这个模块必须包含一个 go.mod 文件。

模块路径的重要性

模块路径不仅用于作为包的导入前缀,还帮助 go command 查找和下载模块。例如,导入 golang.org/x/tools 时,go command 会从 https://golang.org/x/tools 下载该模块。

初始化一个模块

要创建一个新的模块,可以按照以下步骤操作:

  • 创建一个目录:

    mkdir hellocd hello
  • 初始化模块:

    go mod init example.com/user/hello

    这将创建一个 go.mod 文件,内容如下:

    module example.com/user/hellogo 1.16
  • 编写 Go 源文件:

    package mainimport "fmt"func main() {    fmt.Println("Hello, world.")}
  • 构建并安装模块:

    go install example.com/user/hello

    这将编译模块并将二进制文件安装到 GOPATHGOBIN 目录中。

  • 模块依赖管理

    Go 提供了 go mod 命令来管理模块依赖。使用 go mod tidy 会下载所有引用但尚未下载的依赖,同时移除不必要的依赖。

    版本控制

    go.mod 文件中可以指定依赖的版本,确保依赖一致性。例如:

    require (    "fmt v1.0.0"    "example.com/user/hello/morestrings v1.2.3")

    测试模块

    Go 提供了 testing 包来编写单元测试。测试文件应以 _test.go 结尾,测试函数应命名为 Test*** 并带有 func(t *testing.T) 的签名。

    测试示例

    编写测试文件:

    package morestringsimport "testing"func TestReverseRunes(t *testing.T) {    cases := []struct {        in, want string    }{        {"Hello, world", "dlrow ,olleH"},        {"Hello, 世界", "界世 ,olleH"},        {"", ""},    }    for _, c := range cases {        got := ReverseRunes(c.in)        if got != c.want {            t.Errorf("ReverseRunes(%q) == %q, want %q", c.in, got, c.want)        }    }}

    运行测试:

    go test

    远程模块引用

    要引用远程模块,可以直接在 import 语句中使用 URL。例如,引用 github.com/google/go-cmp/cmp

    示例

    package mainimport (    "fmt"    "example.com/user/hello/morestrings"    "github.com/google/go-cmp/cmp")func main() {    fmt.Println(morestrings.ReverseRunes("!oG ,olleH"))    fmt.Println(cmp.Diff("Hello World", "Hello Go"))}

    运行:

    go install example.com/user/hellogo mod tidy

    模块管理命令

    获取模块信息

    go mod info example.com/user/hello

    移除模块缓存

    go clean -modcache

    通过以上指南,你可以轻松创建、管理和使用 Go 模块。

    转载地址:http://knyiz.baihongyu.com/

    你可能感兴趣的文章
    Mysql8.0的特性
    查看>>
    MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
    查看>>
    MySQL8修改密码的方法
    查看>>
    Mysql8在Centos上安装后忘记root密码如何重新设置
    查看>>
    Mysql8在Windows上离线安装时忘记root密码
    查看>>
    MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
    查看>>
    mysql8的安装与卸载
    查看>>
    MySQL8,体验不一样的安装方式!
    查看>>
    MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
    查看>>
    Mysql: 对换(替换)两条记录的同一个字段值
    查看>>
    mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
    查看>>
    MYSQL:基础——3N范式的表结构设计
    查看>>
    MYSQL:基础——触发器
    查看>>
    Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
    查看>>
    mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
    查看>>
    mysqldump 参数--lock-tables浅析
    查看>>
    mysqldump 导出中文乱码
    查看>>
    mysqldump 导出数据库中每张表的前n条
    查看>>
    mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
    查看>>
    Mysqldump参数大全(参数来源于mysql5.5.19源码)
    查看>>