Build a formal-looking Golang project on GitHub

Preface

I have been in contact with golang for a long time, but I started writing golang only recently. Although not much to write, but seen golang projects but numerous, from  Kubernetes  and istio to personally participate in the kustomize then Kubernetes many gadgets ecosystem, such as: kubeval , kubedog and so on. From the perspective of project users and contributors, I have come into contact with various golang projects. As a developer, while enjoying the convenience of various open source projects, I also hope to develop a golang project by myself . With my countless experience of reading projects, then I must build a seemingly formal GitHub project.        

 

GoLand settings

There are many online tutorials for installing Go development environment, so I won't introduce them here. This article mainly introduces the settings of the development environment on GoLand . The settings here are mainly performed on MacOS , and other systems may be different.

 

Use Goland IDE vgo

vgo  is a package management tool based on the Go Module specification, similar to the official go mod command tool.

  • 开启 vgoGoLand->Preferences->GO->Go Modules(vgo)
  • Manually modify  go.mod

Where latest is the latest version, GoLand will download the latest dependency code. After the download is successful, it will modify  go.mod  and generate a  go.sum  dependency analysis file.

module github.com/sunny0826/hamal



go 1.12



require (

    github.com/mitchellh/go-homedir latest

    github.com/spf13/cobra latest

    github.com/spf13/viper latest

)
  • update completed

After the update is successful, it will generate  go.sum  file and modify  go.mod  file.

module github.com/sunny0826/hamal



go 1.12



require (

    github.com/mitchellh/go-homedir v1.1.0

    github.com/spf13/cobra v0.0.5

    github.com/spf13/viper v1.4.0

)
  • Use the shortcut key  (option)+ (return)  or click the right mouse button to select  Sync packages of github.com/sunny0826/hamal to import the dependencies at the import.

 

Configure proxy

If you want to choose the most persuasive reason for golang , then relying on downloads will definitely get the most votes! At this time a suitable ladder is very important. Without this ladder, the above step cannot be completed at all. Here we mainly introduce the configuration on GoLand , the installation and configuration of Shadowsocks will not be introduced.

GoLand -> Preferences -> Appearance & Behavior -> System Settings -> HTTP Proxy  is set here, don't forget to click  Check connection to  test whether the ladder is built.

Configure go fmt , goimports and golangci-lint    

These three tools are built-in GoLand , it is very simple to set up : GoLand -> Preferences -> Tools -> File Watchers , just click add. After the code is written , the automatic detection of these 3 tools will be automatically triggered. The function of the tool:

  • go fmt  : a unified code formatting tool.
  • golangci-lint  : static code quality inspection tool for package quality analysis.
  • goimports  : automatic import dependency package tool.

 

Install and configure golint 

GoLand does not have its own  golint  tool, you need to install it manually:

mkdir -p $GOPATH/src/golang.org/x/

cd $GOPATH/src/golang.org/x/

git clone https://github.com/golang/lint.git

git clone https://github.com/golang/tools.git

cd $GOPATH/src/golang.org/x/lint/golint

go install

After the installation is successful, you will  see the golint binary tool file automatically generated in the $GOPATH/bin  directory  . 

GoLand configure  golint , modify the  three configurations of Name Program and  Arguments  .  Arguments  need to add the  -set_exit_status parameter, as shown in the figure:

 

 

Travis CI continuous integration

How to install Travis CI on Github , log in to Travis CI directly, log in with GitHub , then select the project that needs to use Travis CI , and add .travis.yml to the project root directory , as follows:   

language: go



go:

  - 1.12.5



sudo: required



install:

  - echo "install"



script:

  - echo "script"

Here is just an example, each push after the code will trigger CI , concrete syntax can be found in the official documentation .

Key point:  Do you think using Travis CI is for continuous integration? That would be naive! Use Travis CI, of course, for his Badges ,  just copy RESULT  into your  README.md  .

 

 

GO Report Card

__ another key __ : We GoLand installed on  golint  tools such as code quality testing, code checks can be carried out in line and time code, then this is for a loaded to force. GO Report Card  is a golang code detection website, you only need to fill in the Github address. Get Badges method and Travis CI Similarly, the MarkDown content copied to  RERADME.md  the like.

 

 

GoReleaser

With continuous integration and code inspection, here is how to release a beautiful release . If you are still manually releasing the release , then it will be low again. Use GoReleaser one-line command to release a beautiful release .

Due to the MacOS used,  brew is  used here to install:

brew install goreleaser

Generate  .goreleaser.yml  configuration in the project root directory :

goreleaser init

After configuration, remember to  add dist to .gitignore  , because goreleaser will output the compiled files to the dist directory by default .   

After goreleaser is configured, you can compile and test it first:

goreleaser --skip-validate --skip-publish --snapshot

Note:  To configure GITHUB_TOKEN when using goreleaser for the first time , you can apply here. After applying, run the following command to configure GITHUB_TOKEN

export GITHUB_TOKEN=<YOUR_TOKEN>

Make sure there is no problem, then you can operate git and goreleaser to release the release .

git add .

git commit -m "add goreleaser"

git tag -a v0.0.3 -m "First release"

git push origin master

git push origin v0.0.3

Once everything is done, one line of command takes off:

goreleaser

Goreleaser  works better with CI , so I won’t introduce it here.

 

 

Badges display artifact

Here is an artifact that showcases Badges : https://shields.io/  . This website provides a variety of Badges , if you want, you can fill up your GitHub README.md , and interested students can pick them up .

 

Manuscript source: Alibaba Cloud Developer Community

 

 

 

Guess you like

Origin blog.csdn.net/weixin_40050195/article/details/97130032