Test
你可以覆盖 100% 的代码行,但你永远无法覆盖 100% 的输入和状态组合
- 单元测试 (Unit Testing): 验证开发者对代码单元在预期输入下的行为。
- 模糊测试 (Fuzz Testing): 通过自动生成大量随机或变异输入,探索代码的边缘情况和未知缺陷。Go 1.18 已将 Fuzz Testing 内置到标准工具链中,这是一个强大的武器。
- 集成测试 (Integration Testing): 验证模块间的交互。
- 端到端测试 (End-to-End Testing): 模拟真实用户场景。
- 生产测试/灰度发布 (Staged Rollouts / Canary Releases): 在真实生产环境中,小范围、逐步地验证变更的可靠性,这是大型系统发布的“金丝雀”。
command
go test -v
go test -cover
1 | 测试指定目录下的某个方法 |
Tool
install
1
go install github.com/go-resty/resty/v2@xxx
errcheck
1
errcheck .
format
1
gofmt -w yourfile.go
mod
go mod tidy
is used to clean up the module directory by removing any unnecessary files, such as: Unused dependencies, Duplicate entries, Empty directoriesgo mod vendor
is a part of Go’s vendor tooling, which allows you to manage dependencies in your project by creating a separate directory calledvendor
. This directory contains all the dependencies required for your project.Purpose:
go mod tidy
is used for cleaning up the module directory, whilego mod vendor
is used for managing dependencies in a separatevendor
directory.Output:
go mod tidy
updates thego.mod
file, whilego mod vendor
creates or updates thevendor
directory.run
1
go run /path/to/main/directory