golang 管道_必须具有用于golang构建管道的工具

golang 管道

Build pipelines have become one of the most important parts of every software development company, be it a new startup or an established tech giant.

无论是新成立的初创企业还是成熟的技术巨头,构建管道都已成为每个软件开发公司最重要的组成部分之一。

With CI/CD, your code may go at any time in production, hence you need your build tools to cover and find any potential problems that may have got missed by the developer or reviewer.

使用CI / CD,您的代码可以随时投入生产,因此您需要使用构建工具来覆盖并查找开发人员或审阅者可能遗漏的任何潜在问题。

Here are some potential tools that can not only save you some production problems but will also help with code cleanliness, typos, test coverage, and more.

这里有一些潜在的工具,这些工具不仅可以为您节省一些生产问题,而且还有助于代码清洁度,错别字,测试覆盖率等。

单元测试 (Unit Testing)

Unit testing is an important part of most go applications. Here are some tools which will make it easier

单元测试是大多数go应用程序的重要组成部分。 这里有一些工具,将使其更容易

Golang provides a built-in tool for performing unit tests on your go project. All you need to do is define your test functions in *_test.go files and run the following command in your project dir.

Golang提供了一个内置工具,可以在go项目中执行单元测试。 您需要做的就是在* _test.go文件中定义测试功能,然后在项目目录中运行以下命令。

$ go test -run ' ' #runs all tests

You can also run specific test cases by matching names of test cases as shown

您还可以通过匹配测试用例的名称来运行特定的测试用例,如下所示

$ go test -run Foo     # Run top-level tests matching "Foo", such as "TestFooBar".

Test Coverage

测试覆盖率

Test coverage is a technique that determines whether our test cases are actually covering the code and how much code is exercised when we run those test cases.

测试覆盖率是一种确定我们的测试用例是否真正覆盖了代码以及在运行这些测试用例时执行了多少代码的技术。

Go-test is capable of calculating test coverage per package and output it in various formats for you to analyze.

Go-test能够计算每个包装的测试覆盖率,并以各种格式输出以供您分析。

Checking test coverage for a package is easy

检查包装的测试范围很容易

$ go test -cover
PASS
coverage: 82.9% of statements
ok size 0.036s

But you may want more than that. Golang allows you to profile all your tests and view what code is untested.

但是您可能想要的还不止这些。 Golang允许您分析所有测试并查看未测试的代码。

$ go tool cover -func=coverage.out
size.go: Size 42.9%
total: (statements) 42.9%$ go tool cover -html=coverage.out

This command will open a browser window showing all the code which is not covered by the test cases. You can get this output for all the packages in your project and combine them.

此命令将打开一个浏览器窗口,显示所有未包含在测试用例中的代码。 您可以获取项目中所有软件包的输出,并将它们组合在一起。

$ for d in $(go list ./... | grep -v vendor); do 
go test -race -coverprofile=profile.out "$d";
if [ -f profile.out ]; then
cat profile.out >> coverage.txt;
rm profile.out;
fi;
done;

For more info, head over to the official Golang blog

有关更多信息,请转到Golang官方博客

整理和格式化(Linting and formatting)

Go Fmt is a tool that can automatically format your go code and can also detect unformatted files if needed. To format your code run

Go Fmt是一种工具,可以自动格式化您的go代码,并根据需要检测未格式化的文件。 格式化代码运行

gofmt -w yourcode.go

If formatting and pushing updated code is too difficult in the pipeline, you can throw an error with a list of files that are not formatted properly.

如果在管道中格式化和推送更新的代码太困难,则可能会出现错误,并列出未正确格式化的文件列表。

GOFMT_OUTPUT="$(gofmt -l . 2>&1)"        
if [ -n "$GOFMT_OUTPUT" ]; then
echo "All the following files are not correctly formatted"
echo "${GOFMT_OUTPUT}"
exit 1
fi

Golangci-lint is a linting tool made by the open-source community that aggregates various linters. It can detect unused variables, imports, unhandled errors, dead code, security flaws, and much more.

Golangci-lint是由开源社区制作的整理工具,用于汇总各种短绒。 它可以检测未使用的变量,导入,未处理的错误,无效代码,安全漏洞等。

To run golangci-lint, execute

要运行golangci-lint,请执行

$ 

You can also specify various configuration and select linters that you want to run on your code.

您还可以指定各种配置,并选择要在代码上运行的linter。

For more info, head over to the docs

有关更多信息,请转到文档

MissSpell is a general spell check tool for code, it does not works very well for Golang but does well with docs and comments in go code. Running it is easy

MissSpell是用于代码的常规拼写检查工具,它不适用于Golang,但可以与go代码中的文档和注释一起使用。 运行起来很容易

misspell -error -source=go dir/

Check out the repo for more info

查看仓库以获取更多信息

发布(Release)

GoReleaser automates releases for your application. This is especially helpful when you are building an open-source project. It can build binaries for all the architectures and automatically create git tags and releases for you. It also integrates well with jenkins, gitlabs , github so it can be nice addition to your pipeline.

GoReleaser使您的应用程序的发布自动化。 当您构建一个开源项目时,这特别有用。 它可以为所有架构构建二进制文件,并自动为您创建git标签和发行版。 它还与jenkins,gitlabs和github很好地集成在一起,因此可以很好地添加到您的管道中。

Head over to the quickstart to try it out

前往快速入门进行尝试

Appendix

附录

翻译自: https://medium.com/@arpitkh96/must-have-tools-for-your-golang-build-pipeline-deeee8f6b2d

golang 管道

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值