Help us shape the future of Go! Take the 2026 Go Developer Survey

godbc

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2025 License: Apache-2.0 Imports: 3 Imported by: 204

README

Build Status

godbc

Design by contract for Go

Installation

To install godbc, use go get:

go get github.com/lpabon/godbc

Import the godbc package into your code using this template:

import (
  "github.com/lpabon/godbc"
)

Documentation

Documentation is available at https://godoc.org/github.com/lpabon/godbc

Documentation

Overview

Design-by-Contract for Go

Design by Contract is a programming methodology which binds the caller and the function called to a contract. The contract is represented using Hoare Triple:

{P} C {Q}

where {P} is the precondition before executing command C, and {Q} is the postcondition.

See Also

* http://en.wikipedia.org/wiki/Design_by_contract * http://en.wikipedia.org/wiki/Hoare_logic * http://dlang.org/dbc.html

Usage

Godbc is enabled by default, but can be disabled for production builds by using the tag 'prod' in builds and tests as follows:

go build -tags 'prod'

or

go test -tags 'prod'

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(b bool, message ...interface{})

Check provides a simple assert

func Ensure

func Ensure(b bool, message ...interface{})

Ensure checks the postconditions are satisfied before returning to the caller.

Example Code

type Data struct {
	a int
}

func (*d Data) Set(a int) {
	d.a = a
	godbc.Ensure(d.a == a)
}

func Invariant

func Invariant(obj InvariantTester, message ...interface{})

Invariant calls the objects Invariant() receiver to test the object for correctness.

The caller object must provide an object that supports the interface InvariantTester

To see an example, please take a look at the godbc_test.go

func InvariantSimple

func InvariantSimple(obj InvariantSimpleTester, message ...interface{})

InvariantSimple calls the objects Invariant() receiver to test the object for correctness.

The caller object must provide an object that supports the interface InvariantSimpleTester and does not need to provide a String() receiver

func Require

func Require(b bool, message ...interface{})

Require checks that the preconditions are satisfied before executing the function

Example Code

func Divide(a, b int) int {
	godbc.Require(b != 0)
	return a/b
}

Types

type InvariantSimpleTester

type InvariantSimpleTester interface {
	Invariant() bool
}

InvariantSimpleTester is an interface which provides a receiver to test the object

type InvariantTester

type InvariantTester interface {
	InvariantSimpleTester
	String() string
}

InvariantTester is an interface which provides not only an Invariant(), but also a receiver to print the structure

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL