github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/docs/source/style-guides/go-style.rst (about) 1 Coding guidelines 2 ----------------- 3 4 Coding in Go 5 ~~~~~~~~~~~~ 6 7 We code in Go™ and try to follow the best practices and style outlined in 8 `Effective Go <https://golang.org/doc/effective_go.html>`__ and the 9 supplemental rules from the `Go Code Review Comments wiki 10 <https://github.com/golang/go/wiki/CodeReviewComments>`__. 11 12 We also recommend new contributors review the following before submitting 13 pull requests: 14 15 - `Practical Go <https://dave.cheney.net/practical-go/presentations/qcon-china.html>`__ 16 - `Go Proverbs <https://go-proverbs.github.io/>`__ 17 18 The following tools are executed against all pull requests. Any errors flagged 19 by these tools must be addressed before the code will be merged: 20 21 - `gofmt -s <https://golang.org/cmd/gofmt/>`__ 22 - `goimports <https://godoc.org/golang.org/x/tools/cmd/goimports>`__ 23 - `go vet <https://golang.org/cmd/vet/>`__ 24 25 Testing 26 ^^^^^^^ 27 28 Unit tests are expected to accompany all production code changes. These tests 29 should be fast, provide very good coverage for new and modified code, and 30 support parallel execution. 31 32 Two matching libraries are commonly used in our tests. When modifying code, 33 please use the matching library that has already been chosen for the package. 34 35 - `gomega <https://onsi.github.io/gomega/>`__ 36 - `testify/assert <https://godoc.org/github.com/stretchr/testify/assert>`__ 37 38 Any fixtures or data required by tests should generated or placed under version 39 control. When fixtures are generated, they must be placed in a temporary 40 directory created by ``ioutil.TempDir`` and cleaned up when the test 41 terminates. When fixtures are placed under version control, they should be 42 created inside a ``testdata`` folder; documentation that describes how to 43 regenerate the fixtures should be provided in the tests or a ``README.txt``. 44 Sharing fixtures across packages is strongly discouraged. 45 46 When fakes or mocks are needed, they must be generated. Bespoke, hand-coded 47 mocks are a maintenance burden and tend to include simulations that inevitably 48 diverge from reality. Within Fabric, we use ``go generate`` directives to 49 manage the generation with the following tools: 50 51 - `counterfeiter <https://github.com/maxbrunsfeld/counterfeiter>`__ 52 - `mockery <https://github.com/vektra/mockery>`__ 53 54 API Documentation 55 ^^^^^^^^^^^^^^^^^ 56 57 The API documentation for Hyperledger Fabric's Go APIs is available 58 in `GoDoc <https://godoc.org/github.com/hyperledger/fabric>`_. 59 60 Adding or updating Go packages 61 ------------------------------ 62 63 Hyperledger Fabric uses go modules to manage and vendor its dependencies. This 64 means that all of the external packages required to build our binaries reside 65 in the ``vendor`` folder at the top of the repository. Go uses the packages in 66 this folder instead of the module cache when ``go`` commands are executed. 67 68 If a code change results in a new or updated dependency, please be sure to run 69 ``go mod tidy`` and ``go mod vendor`` to keep the ``vendor`` folder and 70 dependency metadata up to date. 71 72 See the `Go Modules Wiki <https://github.com/golang/go/wiki/Modules>`__ for 73 additional information. 74 75 .. Licensed under Creative Commons Attribution 4.0 International License 76 https://creativecommons.org/licenses/by/4.0/