cog

package module
v0.0.0-...-f5c3557 Latest Latest
Warning

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

Go to latest
Published: May 2, 2018 License: MIT Imports: 4 Imported by: 0

README

Cog Build Status

Cog is a collection of utils for golang that I tend to use across many of my projects. Rather than building new cogs everywhere, I've just consolidated them all here. Cogs for everyone!

Modules

Cog consists of the following modules:

Module Docs Description
(root) GoDoc generic utils that didn't fit anywhere else
bytec GoDoc extra byte slice utils
cfs GoDoc filesystem utils
check GoDoc test assertions and isolated FS utils
cio GoDoc extra io utils
clog GoDoc a logging framework that looks a bit like python's logging
cnet GoDoc misc net utils and a socket implementation using channels
cort GoDoc extra sorting utilities
ctime GoDoc time utils
cync GoDoc some extra sync utils
encoding/capn GoDoc capnproto Marshaling and Unmarshaling
encoding/path GoDoc path Marshaling and Unmarshaling
node GoDoc get information about the local node
stack GoDoc runtime call stack utils
statc GoDoc application status and stats
stringc GoDoc extra strings utils
unsafec GoDoc making things more unsafe

Each module contains full documentation over on godoc, including tons of examples.

As you might have noticed, the modules have weirdly spelled names; this is so that you can, for example, import both "sync" and "cync" into the same file, since "cync" only supplements "sync".

Documentation

Overview

Package cog is a collection of utilities that I tend to use across many of my projects. Rather than reinventing the cog everywhere, I've just consolidated them all here.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert(ok bool, msg string, args ...interface{})

Assert ensures ensures that the given bool is true, or panics.

func BytesMust

func BytesMust(b []byte, err error) []byte

BytesMust ensures that a function that returns a ([]byte, error) does not error.

func Must

func Must(err error, msg string, args ...interface{})

Must ensures that no error occurred, or panics.

func Notify

func Notify(ch chan<- struct{})

Notify sends a struct{} down the channel without waiting

Types

type Errors

type Errors struct {
	// contains filtered or unexported fields
}

Errors holds a list of errors that can be combined into a single, line- delimited error message

Example
es := Errors{}

// Nothing added
es.Add(nil)
es.Addf(nil, "something describing the error: %d", 123)
fmt.Println("no errors:", es.Error())

// Errors logged
es.Add(fmt.Errorf("some error"))
es.Addf(fmt.Errorf("another error"), "something describing the error: %d", 123)
fmt.Println("errors:", es.Error())
Output:

no errors: <nil>
errors: some error
something describing the error: 123: another error

func (*Errors) Add

func (es *Errors) Add(err error)

Add adds an error to the collector. It's safe to call this when err==nil.

func (*Errors) Addf

func (es *Errors) Addf(err error, format string, args ...interface{})

Addf is like Add, but prefixes the error with the given format and args. If err==nil, this does nothing.

func (*Errors) Drain

func (es *Errors) Drain(ch <-chan error)

Drain removes every error from the given channel until the channel closes

func (*Errors) Empty

func (es *Errors) Empty() bool

Empty checks to see if any errors have been added

func (*Errors) Error

func (es *Errors) Error() error

Error combines all previous errors into a single error, or returns nil if there were no errors.

func (*Errors) Prefix

func (es *Errors) Prefix(prefix string) *Errors

Prefix returns an Errors for which all errors are prefixed with `prefix`.

func (*Errors) Reset

func (es *Errors) Reset()

Reset clears the state of Errors for use again

type Exit

type Exit struct {
	*GExit
	// contains filtered or unexported fields
}

Exit is useful for terminating a group of goroutines that run in a for{select{}}. Be sure to `Exit.Add(n)` before starting goroutines, and `defer Exit.Done()` in the goroutine.

Example
e := NewExit()

run := func(i int) {
	e.Add(1)
	fmt.Println(i, "started")

	go func() {
		defer e.Done()
		defer fmt.Println("exited")

		for {
			select {
			case <-e.C:
				return
			}
		}
	}()
}

for i := 0; i < 10; i++ {
	run(i)
}

// Wait for all goroutines to exit
e.Exit()
Output:

0 started
1 started
2 started
3 started
4 started
5 started
6 started
7 started
8 started
9 started
exited
exited
exited
exited
exited
exited
exited
exited
exited
exited

func NewExit

func NewExit() *Exit

NewExit creates a new Exit, useful for ensuring termination of goroutines on exit.

func (*Exit) Exit

func (e *Exit) Exit()

Exit closes C and waits for all goroutines to exit.

func (*Exit) Signal

func (e *Exit) Signal()

Signal tells everything to exit, but it doesn't wait.

type Exiter

type Exiter interface {
	Exit()
}

Exiter is anything that can cleanup after itself at any arbitrary point in time.

type GExit

type GExit struct {
	sync.WaitGroup
	C <-chan struct{}
	// contains filtered or unexported fields
}

GExit (short for "goroutine exit") is what should be passed to things that need to know when to exit but that should not be able to trigger an exit.

func (*GExit) AddCb

func (e *GExit) AddCb(cb func())

AddCb adds a function to be called on exit. Callbacks are called in the reverse order that they were added.

func (*GExit) AddExiter

func (e *GExit) AddExiter(ex Exiter)

AddExiter adds an Exiter to the exit list that is called when Exit() is called. Exiters are called in the reverse order that they were added.

Directories

Path Synopsis
Package bytec implements some bytes utilities.
Package bytec implements some bytes utilities.
Package cfs implements some extra filesystem utilities
Package cfs implements some extra filesystem utilities
Package check provides dead-simple assertions and utilities for testing.
Package check provides dead-simple assertions and utilities for testing.
chlog
Package chlog provides clog-based logging for testing Usage is really simple: import "github.com/iheartradio/cog/check/chlog" func TestStuff(t *testing.T) { log := chlog.New(t) }
Package chlog provides clog-based logging for testing Usage is really simple: import "github.com/iheartradio/cog/check/chlog" func TestStuff(t *testing.T) { log := chlog.New(t) }
cio
Package cio implements extra io utils
Package cio implements extra io utils
eio
Package eio implements extensible, async io backends.
Package eio implements extensible, async io backends.
eio/kafka
Package kafka implements an eio producer and consumer for kafka.
Package kafka implements an eio producer and consumer for kafka.
Package clog implements a python-like, module-based logger with a variety of backends and formats.
Package clog implements a python-like, module-based logger with a variety of backends and formats.
cmd
Package cnet provides more network utils, including sockets made from channels.
Package cnet provides more network utils, including sockets made from channels.
Package cort implements extra sort helpers.
Package cort implements extra sort helpers.
Package ctime implements extra time utilities.
Package ctime implements extra time utilities.
Package cync implements some sync extras "cync" is pronounced "sync".
Package cync implements some sync extras "cync" is pronounced "sync".
encoding
capnp
Package capnp implements some stupid stuff for capnproto
Package capnp implements some stupid stuff for capnproto
path
Package path provides Marshaling and Unmarshaling for [ ]byte-encoded paths.
Package path provides Marshaling and Unmarshaling for [ ]byte-encoded paths.
Package node provides information about the local node
Package node provides information about the local node
Package stack provides some utilities for dealing with the call stack.
Package stack provides some utilities for dealing with the call stack.
Package statc implements runtime process stats and status reporting.
Package statc implements runtime process stats and status reporting.
Package stringc implements some strings extras "stringc" is pronounced "strings".
Package stringc implements some strings extras "stringc" is pronounced "strings".
Package unsafec makes unsafe even more unsafe.
Package unsafec makes unsafe even more unsafe.

Jump to

Keyboard shortcuts

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