Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


A tiny library that contains utilities for checking functions' preconditions in Go programming language. Precondition checking is a concept intruduced in code design approach called Design by Contract. If preconditions of a function are not satisfied then, according to Crash Early Principle, program is invalid and should be immediately terminated. Termination should be preceeded with error message. Precond simplifies doing it all (contract checking, printing error and crashing) to a single function call.

Fetch muroc/prego library to your go workspace!

go get

How To Use It

As many other languages, Go does not provide nillability information in its type system. Precond fills this gap by providing precond.NotNil function. Checking if argument is not nil is the most simple and most common way of using the library.

import ""

func trim(noBeTrimmed string) string {
  // if toBeTrimmed is nil, precond.NotNil will panic
  // with error passed as second argument
  precond.NotNil(toBeTrimmed, "trimmed string must not be null")

  ... // function body

Error Formatting

All methods in the precond and check namespace support passing arguments to be used when formatting error message. fmt.Errorf is used to create error instances. All fmt formatting rules apply.

import ""

func (set *Set) get(index int) interface{} {
  // if index is not contained in <0, length> precond.InRange
  // eill panic with properly formatted error
  precond.InRange(index, 0, set.Length()-1, "index %v out of bounds", index)

  ... // function body

Other panic checks can be found in panic.go.

Defensive Programming

Package precond/check contain function that does the same checks, but return encountered errors instead of panic. They will come in handy when writing code that uses technique called Defensive Programming.

import ""

func handleMessage(msg string) string {
  // if isProerFormat(msg) is false, check.True will return error
  // with message passed as second argument
  err := check.True(isProperFormat(msg), "ill-formated message: %v", message)
  if err != nil {

  ... // function body

Other error checks can be found in check/check.go.


Copyright © 2016 Maciej Chałapuk. Released under Apache License 2.0.


Precondition checks for Go language; Useful in contract programming







No packages published