Documentation ¶
Overview ¶
Go By Contract provides a lightweight mechanism to validate Design-by-Contract pre-conditions and post-conditions in production environments by using Sentry error reporting instead of "fail hard" behaviour.
This small library demonstrates the pattern described in the following blog post: https://icyapril.com/go/programming/2019/03/25/a-pattern-for-validating-design-by-contract-in-Production-with-go-and-sentry.html
Setting the GOBYCONTRACT_DONTPANIC environment variable disables panics for a production environment when set to "true", whilst the SENTRY_DSN environment will independently enable reporting to Sentry.
The following is an example of using Require for preconditions and Ensure for postconditions:
func SecondsToSecondsAndMinutes(seconds int) (minutes int, remainingSeconds int) { gobycontract.Require(seconds >= 0, "Input seconds must be positive") minutes = seconds/60 remainingSeconds = seconds % 60 gobycontract.Ensure(minutes >= 0, "Output minutes must be positive") gobycontract.Ensure(remainingSeconds >= 0, "Output remaining seconds must be positive") gobycontract.Ensure(remainingSeconds < 60, "There can be no more than 59 remaining seconds") return }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
This section is empty.