Git Product home page Git Product logo

goguide's Introduction

Go coding conventions

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

The sections and conventions are ordered alphabetically.

General

These conventions closely follow Go idioms, Go best practicies, and the Go Way. They are the consensus of the Go community on how best to write Go. Most who write Go already do these things. Aside from the obvious benefits, it is important to write Go like the Go community so new engineers can more easily understand it.

MUST comply with go build ./...

MUST comply with go test ./...

MUST comply with go vet ./...

MUST comply with gofmt -s

MUST comply with goimports

MUST comply with golint

MUST support go doc

MUST support go generate

MUST support go get

MUST support go install

MUST support go mod

SHOULD comply with https://golang.org/blog

SHOULD follow the example of the Go project

SHOULD follow the example of the Go standard libraries

Style

SHOULD begin one-line comments and end one-line general comments with a space

Examples

Good:

// Started earlier
/* Started earlier */

Bad:

//Started earlier
/*Started earlier*/

SHOULD end comments with multiple sentences with punctuation

Examples

Good:

// Started earlier. Skip initialization.

Bad:

// Started earlier. Skip initialization

SHOULD use sentence case for whole-line comments

Examples

Good:

// Started earlier
if stopped() {

Bad:

// started earlier
if stopped() {

Testing

SHOULD NOT use test suites

Some are tempted to use test suites on top of the standard Go test framework, like testify/suite.

Some reasons to use plain Go tests:

  • It's simpler
  • It's fully capable
  • It's widely understood
  • It's built-in
  • It's supported by the Go team, stable, idiomatic, and high-quality
  • It's not a DSL

Some reasons not to use testify/suite:

  • It encourages shared state among test methods, which is difficult to reason about, extend, and parallelize. Most tests should be independent and able to run in parallel. The motivation for using suites goes away as soon as you isolate every test.
  • You have to have a driver test in the Go test framework just to get it to work:
func TestFooTestSuite(t *testing.T) {
	suite.Run(t, new(FooTestSuite))
}
  • You have to write boilerplate just to do simple testing:
type FooTestSuite struct {
	suite.Suite
}

func (suite *FooTestSuite) TestFoo() {
	...
}

func TestFooTestSuite(t *testing.T) {
	suite.Run(t, new(FooTestSuite))
}

Compare that to:

func TestFoo(t *testing.T) {
	...
}
  • It's a third-party, external dependency that we have to learn and maintain

If you look at suite.Suite, it doesn't actually do all that much:

func (suite *Suite) Assert() *assert.Assertions
func (suite *Suite) Require() *require.Assertions
func (suite *Suite) Run(name string, subtest func()) bool
func (suite *Suite) SetT(t *testing.T)
func (suite *Suite) T() *testing.T

It just packages together shared assert.Assertions, require.Assertions, and testing.T values, and provides a helper method for calling testing.T.Run. suite.Run provides support for setup/teardown hooks. That's it. Go tests already have a testing.T, they can already use assert.Assertions and require.Assertions, and—as illustrated in the testing package doc1, 2—it's trivial enough to do setup/teardown with normal Go code and subtests.

goguide's People

Contributors

willfaught avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.