go import package 时,使用的是目录名称和 go.mod 里的 module 名称。
- 目录内有 go.mod 文件,则使用 go.mod 里的 module 定义的名称。
- 目录内没有 go.mod 文件,则使用 目录名称。
调用 package 时,使用的是 package 定义的名称和 import 定义的别名。
go.mod
module github.com/pelletier/go-tomlgo 1.12require (github.com/BurntSushi/toml v0.3.1github.com/davecgh/go-spew v1.1.1gopkg.in/yaml.v2 v2.2.8)
定义包
package toml
导入包
import "github.com/pelletier/go-toml"
使用包
type Postgres struct {User stringPassword string}type Config struct {Postgres Postgres}doc := []byte(`[Postgres]User = "pelletier"Password = "mypassword"`)config := Config{}toml.Unmarshal(doc, &config)fmt.Println("user=", config.Postgres.User)
参考
go-toml
