A Go tool that automatically generates pre- and post-condition checks from function descriptions to implement design-by-contract.
gocontracts is a design-by-contract tool for Go that synchronizes contract specifications in documentation with runtime checks in code. It parses function descriptions to generate and update pre-condition, post-condition, and preamble code blocks, ensuring contracts are both documented and enforced. This approach helps maintain consistency between documentation and implementation.
Go developers and teams implementing formal design-by-contract methodologies in their codebases, particularly those needing automated synchronization of documented invariants with actual runtime checks.
Developers choose gocontracts because it uniquely synchronizes contract documentation with generated code, avoiding drift between specs and implementation. Unlike alternatives, it supports condition labels, initialization statements, state transitions via preambles, and build-time toggling, all while emphasizing logical invariants over input validation.
A tool for design-by-contract in Go
Parses function docstrings to generate and update pre-condition, post-condition, and preamble code blocks automatically, reducing manual coding effort.
Ensures contract specifications in documentation are always reflected in runtime checks, preventing drift between specs and implementation as highlighted in the README.
Allows capturing pre-execution state via preamble blocks for contracts that depend on before/after comparisons, enabling formal state transition checks.
Supports conditional enabling of contracts using build tags and boolean constants, allowing flexible deployment across testing and production environments.
Does not check for syntax errors or undefined variables in conditions, inserting code as-is which can lead to runtime panics if conditions are malformed, as admitted in the README.
Requires developers to manually import packages used in contract conditions, since the tool does not manage dependencies, adding extra overhead.
Condition labels are limited to a specific character set [a-zA-Z0-9_;.\-=' ], which may not cover all naming conventions and could cause ambiguity in documentation.
Automatically generate Go test boilerplate from your source code.
XSD (XML Schema Definition) parser and Go/C/Java/Rust/TypeScript code generator
Haskell-flavoured functions for Go :smiley:
Generics for go
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.