Created
June 9, 2013 07:28
-
-
Save karlseguin/5742394 to your computer and use it in GitHub Desktop.
Testable time.Now in GoLang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package t | |
import ( | |
"time" | |
) | |
type NowFunc func() time.Time | |
var Now = func() time.Time { return time.Now() } | |
func NowForce(unix int) { | |
Now = func() time.Time { return time.Unix(int64(unix), 0) } | |
} | |
// use t.Now() throughout your code | |
// and force a specific timestamp via t.NowForce(1233) in tests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this a typo? NowFunc / NowForce