0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Fake Time

Posted at

Overview

Fake Time for test. not best way.

Detail


var now = Time.Now

// if you want to use specify time, you call it. then replace time to argument valuen.
// then, you receive function is for revert original time module. for finish test, recommend call by defer
func switchFakeTime(t time.Time) (revertFakeTime func()) {
	now = func() time.Time {
		return t
	}

	return func() {
		now = time.Now
	}
}


fmt.Println(now())

revertFunc := switchFakeTime.(Time.Now())

fmt.Println(now())
time.Sleep(10 * time.Second)
fmt.Println(now())

revertFunc()
fmt.Println(now())

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?