$ grep 'package ' ~/.asdf/installs/golang/1.21.2/go/src/fmt/* ~/.asdf/installs/golang/1.21.2/go/src/fmt/doc.go:and the package does not protect against them. ~/.asdf/installs/golang/1.21.2/go/src/fmt/doc.go:print routine, the fmt package reformats the error message ~/.asdf/installs/golang/1.21.2/go/src/fmt/doc.go:package fmt ~/.asdf/installs/golang/1.21.2/go/src/fmt/errors.go:package fmt ~/.asdf/installs/golang/1.21.2/go/src/fmt/errors_test.go:package fmt_test ~/.asdf/installs/golang/1.21.2/go/src/fmt/example_test.go:package fmt_test ~/.asdf/installs/golang/1.21.2/go/src/fmt/example_test.go:// rather than its value. The examples are not exhaustive; see the package comment ~/.asdf/installs/golang/1.21.2/go/src/fmt/export_test.go:package fmt ~/.asdf/installs/golang/1.21.2/go/src/fmt/fmt_test.go:package fmt_test ~/.asdf/installs/golang/1.21.2/go/src/fmt/format.go:package fmt ~/.asdf/installs/golang/1.21.2/go/src/fmt/gostringer_example_test.go:package fmt_test ~/.asdf/installs/golang/1.21.2/go/src/fmt/print.go:package fmt ~/.asdf/installs/golang/1.21.2/go/src/fmt/scan.go:package fmt ~/.asdf/installs/golang/1.21.2/go/src/fmt/scan.go: // for the operation being performed; see the package documentation ~/.asdf/installs/golang/1.21.2/go/src/fmt/scan.go: // performed; see the package documentation for more information. ~/.asdf/installs/golang/1.21.2/go/src/fmt/scan.go:// to avoid depending on package unicode. ~/.asdf/installs/golang/1.21.2/go/src/fmt/scan_test.go:package fmt_test ~/.asdf/installs/golang/1.21.2/go/src/fmt/state_test.go:package fmt_test ~/.asdf/installs/golang/1.21.2/go/src/fmt/stringer_example_test.go:package fmt_test ~/.asdf/installs/golang/1.21.2/go/src/fmt/stringer_test.go:package fmt_test
无法导入 main 包,会编译报错 - package main is not in std
hello.go
1 2 3
package hello
import"main"
1 2
$ go build hello.go hello.go:3:8: package main is not in std (~/.asdf/installs/golang/1.21.2/go/src/main)
Go 源文件本身采用 Unicode 字符集,并采用 UTF-8 字符编码 与编译后的程序所运行环境所使用的字符集和字符编码是一致的
Go Module 是从 Go 1.11 正式引入的,用于解决 Go 项目复杂的依赖问题 从 Go 1.16 开始,成为默认的包依赖管理机制和源码构建机制
1 2 3 4 5 6 7
$ tree . └── main.go
$ go build main.go main.go:4:2: no required module provides package github.com/valyala/fasthttp: go.mod file not found in current directory or any parent directory; see 'go help modules' main.go:5:2: no required module provides package go.uber.org/zap: go.mod file not found in current directory or any parent directory; see 'go help modules'
go.mod 存储了 Go Module 对第三方依赖的全部信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$ go mod init github.com/zhongmingmao/hello-go go: creating new go.mod: module github.com/zhongmingmao/hello-go go: to add module requirements and sums: go mod tidy $ tree . ├── go.mod └── main.go
$ go build main.go main.go:4:2: no required module provides package github.com/valyala/fasthttp; to add it: go get github.com/valyala/fasthttp main.go:5:2: no required module provides package go.uber.org/zap; to add it: go get go.uber.org/zap $ go get github.com/valyala/fasthttp go: added github.com/andybalholm/brotli v1.0.5 go: added github.com/klauspost/compress v1.16.3 go: added github.com/valyala/bytebufferpool v1.0.0 go: added github.com/valyala/fasthttp v1.50.0