Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Golang Clock Package
package clock
import "time"
// Clock is a wrapper around time functions to make
// time dependent code easily testable.
type Clock struct {
now time.Time
// FromTime returns a new clock with a set time.
func FromTime(t time.Time) *Clock {
return &Clock{now: t}
// Return the current time.
func (c *Clock) Now() time.Time {
if c == nil {
return time.Now()
// Set the current time.
func (c *Clock) SetNow(t time.Time) { = t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment