What's the best practice to infer the methods? - casting

I have a custom time format which I use to properly encode/decode json. However whenever I need to do a time computation I need to do a cast. Is this the right way? It feels a bit ugly to keep casting. For example when I need to "update" the value I need to cast it twice ( once to time and once to my type)
type Mytime time.Time
var t Mytime
t = Mytime(time.Now())
// Add an hour to my typed time
t = Mytime(time.Time(t).Add(1 * time.Hour))

Presumably you have type Mytime time.Time. If instead you embedded it:
type MyTime struct {
time.Time
}
Then you could have:
func (t MyTime) MarshalJSON() ([]byte, error) {
… whatever …
}
and still access all of time.Time's methods.
E.g. something like:
t := MyType{time.Now()}
t.Time = t.Add(time.Hour)
Fuller example showing differences between embedded and non-embedded custom time types. Note that embedding still doesn't allow for transparent inter-use with things that expect a time.Time value. (The reason for making these types, e.g. to add a MarshalJSON method has been omitted here).
package main
import (
"fmt"
"time"
)
type YourTime time.Time
type MyTime struct{ time.Time }
// Some random functions, perhaps in a third party package,
// that deals with time.Time values.
func fn(t time.Time) {
fmt.Println("fn got:", t)
}
func fn2() time.Time {
return time.Unix(14e8, 0)
}
func main() {
var t1 = YourTime(time.Now())
//t1 = t1.Add(time.Hour) // compiler error
t1 = YourTime(time.Time(t1).Add(time.Hour))
fmt.Println("ugly t1:", t1)
fmt.Println("nice t1:", time.Time(t1))
//fn(t1) // compiler error
fn(time.Time(t1))
//t1 = fn2() // compiler error
t1 = YourTime(fn2())
var t2 = MyTime{time.Now()}
// t2 = t2.Add(time.Hour) // compiler error
t2.Time = t2.Add(time.Hour)
fmt.Println("t2:", t2)
//fn(t2) // compiler error
fn(t2.Time)
//t2 = fn2() // compiler error
t2.Time = fn2()
}
Playground
Output:
ugly t1: {63393494400 0 0x1c9340}
nice t1: 2009-11-11 00:00:00 +0000 UTC
fn got: 2009-11-11 00:00:00 +0000 UTC
t2: 2009-11-10 23:00:00 +0000 UTC
fn got: 2009-11-10 23:00:00 +0000 UTC

Related

DynamoDB Marshal and unmarshal golang time.Time as millis since epoch

The sdk by default marshals time.Time values as RFC3339 strings. How can you choose to marshal and unmarshal in other ways e.g. millis since epoch?
The SDK mentions the Marshaler and Unmarshaler interfaces but does not explain how to use them.
(As I was about to post my question I figured out the answer by looking into how UnixTime worked).
To use a custom Marshaler and Unmarshaler you can create a custom type.
type MillisTime time.Time
func (e MillisTime) MarshalDynamoDBAttributeValue(av *dynamodb.AttributeValue) error {
millis := timeAsMillis(time.Time(e))
millisStr := fmt.Sprintf("%d", millis)
av.N = &millisStr
return nil
}
func (e *MillisTime) UnmarshalDynamoDBAttributeValue(av *dynamodb.AttributeValue) error {
millis, err := strconv.ParseInt(*av.N, 10, 0)
if err != nil {
return err
}
*e = MillisTime(millisAsTime(millis))
return nil
}
func timeAsMillis(t time.Time) int64 {
nanosSinceEpoch := t.UnixNano()
return (nanosSinceEpoch / 1_000_000_000) + (nanosSinceEpoch % 1_000_000_000)
}
func millisAsTime(millis int64) time.Time {
seconds := millis / 1_000
nanos := (millis % 1_000) * 1_000_000
return time.Unix(seconds, nanos)
}
NOTE: Example above uses the new number literal syntax introduced in go 1.13.
You can easily marshal and unmarshal structs using MarshalMap and UnmarshalMap but the downside is that the fields in your struct type have to use MillisTime instead of time.Time. Conversion is not easy but is possible.
The SDK defines a UnixTime type which will handle marshaling and unmarshaling between time.Time <=> seconds since epoch.

comparing current time in unit test

I'm currently writing a unit test that compares to strings. The first string is generated using a function. The other one is hard coded and serves as reference. My problem is, that the function creating the first string injects the current time (time.Now()) with precision in seconds into the string. At the moment I do the same for the reference but this seems very ugly to me. My machine runs fast enough so that the test passes but I don't want to rely on that.
What are general techniques to do such tests?
You can stub functions like time.Now() in your _test.go files, via the init() function, this will give deterministic time values:
package main
import (
"fmt"
"time"
)
var timeNow = time.Now
func main() {
fmt.Println(timeNow())
}
func init() {
// Uncomment and add to _test.go init()
// timeNow = func() time.Time {
// t, _ := time.Parse("2006-01-02 15:04:05", "2017-01-20 01:02:03")
// return t
// }
}
See: https://play.golang.org/p/hI6MrQGyDA
we can stub time.Now() by using go package "github.com/tkuchiki/faketime".
package main
import (
"fmt"
"github.com/tkuchiki/faketime"
"time"
)
func main() {
fmt.Println("Current Time Before Faking : ", time.Now().UTC())
f := faketime.NewFaketime(2021, time.March, 01, 01, 01, 01, 0, time.UTC)
defer f.Undo()
f.Do()
fmt.Println("Current Time After Faking : ", time.Now())
}
Output from the above code is :
Current Time Before Faking : 2009-11-10 23:00:00 +0000 UTC
Current Time After Faking : 2021-03-01 01:01:01 +0000 UTC
checkout the sample code : go-playground sample

Convert NSUUID to UnsafePointer<UInt8>

Following the update to Swift 3, it appears both getUUIDBytes and getBytes are not available on the UUID object.
let uuid = UIDevice.current.identifierForVendor
let mutableUUIDData = NSMutableData(length:16)
uuid.getBytes(UnsafeMutablePointer(mutableUUIDData!.mutableBytes))
// ^^^ compiler error, value of type UUID? has no member getBytes
I get this error even when getBytes is listed as a method on UUID in the documentation: https://developer.apple.com/reference/foundation/nsuuid/1411420-getbytes
One right way:
let uuid = UIDevice.current.identifierForVendor!
var rawUuid = uuid.uuid
withUnsafePointer(to: &rawUuid) {rawUuidPtr in //<- `rawUuidPtr` is of type `UnsafePointer<uuid_t>`.
rawUuidPtr.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout<uuid_t>.size) {bytes in
//Use `bytes` only in this closure. (Do NEVER export `bytes` out of the closure.)
print(bytes[0],bytes[1])
//...
}
}
Another right way:
withUnsafePointer(to: &rawUuid) {rawUuidPtr in //<- `rawUuidPtr` is of type `UnsafePointer<uuid_t>`.
let bytes = UnsafeRawPointer(rawUuidPtr).assumingMemoryBound(to: UInt8.self)
//Use `bytes` only in this closure. (Do NEVER export `bytes` out of the closure.)
print(bytes[0],bytes[1])
//...
}
As already commented by Rob, exporting the pointer passed to the closure argument of withUnsafeBytes is completely NOT guaranteed. A slight change of the context (32-bit/64-bit, x86/ARM, Debug/Release, adding seemingly unrelated code...) would make your app a crasher.
And one more important thing is that UTF-8 Data of the uuidString and the byte sequence of NSUUID.getBytes are completely different:
let nsUuid = uuid as NSUUID //<-Using the same `UUID`
let mutableUUIDData = NSMutableData(length:16)!
nsUuid.getBytes(mutableUUIDData.mutableBytes.assumingMemoryBound(to: UInt8.self))
print(mutableUUIDData) //-><1682ed24 09224178 a279b44b 5a4944f4>
let uuidData = uuid.uuidString.data(using: .utf8)!
print(uuidData as NSData) //-><31363832 45443234 2d303932 322d3431 37382d41 3237392d 42343442 35413439 34344634>
You are thinking too complicated:
func getUUID ( ) -> Data {
let uuid = NSUUID()
var bytes = [UInt8](repeating: 0, count: 16)
uuid.getBytes(&bytes)
return Data(bytes: bytes)
}
Why does that work?
Consider you have:
func printInt(atAddress p: UnsafeMutablePointer<Int>) {
print(p.pointee)
}
then you can in fact do this:
var value: Int = 23
printInt(atAddress: &value)
// Prints "23"
but you can also do this:
var numbers = [5, 10, 15, 20]
printInt(atAddress: &numbers)
// Prints "5"
It's a form of "implicit bridging". To quote from Swiftdoc.org:
A mutable pointer to the elements of an array is implicitly created
when you pass the array using inout syntax.
This implicit bridging only guarantees valid pointers until the current function returns. Such pointers must never "escape" the current function context, but using them as an inout argument is always safe, as inout arguments were always only guarantee to be valid until the called function returns and the called function must return prior to the current one, so this cannot go wrong.
And for those that don't know, casting UUID to NSUUID (... as NSUUID) and the other way round (... as UUID) is guaranteed to always succeed. But if you insist on using UUID, the easiest way is:
private
func getUUID ( ) -> Data {
var uuid = UUID().uuid
return withUnsafePointer(to: &uuid) {
return Data(bytes: $0, count: MemoryLayout.size(ofValue: uuid))
}
}

Go Lang How to Mock Package Method? [duplicate]

Part of our code is time-sensitive, and we need to able to reserve something and then release it in 30-60 seconds, etc., which we can just do a time.Sleep(60 * time.Second).
I have just implemented the time interface, and during the test I used a stubbed implementation of the time interface, similar to this golang-nuts discussion.
However, time.Now() is called in multiple sites which means we need to pass a variable around to keep track of how much time we have actually slept.
Is there an alternative way to stub out time.Now() globally? Maybe making a system call to change the system clock?
Can we maybe write our own time package which basically wraps around the time package but allows us to change it?
Our current implementation works well. I am a Go beginner, and I am curious to see if anyone has other ideas.
With implementing a custom interface you are already on the right way. I take it you use the following advise from the golang-nuts thread you've posted:
type Clock interface {
Now() time.Time
After(d time.Duration) <-chan time.Time
}
and provide a concrete implementation
type realClock struct{}
func (realClock) Now() time.Time { return time.Now() }
func (realClock) After(d time.Duration) <-chan time.Time { return time.After(d) }
and a testing implementation.
Original
Changing the system time while making tests (or in general) is a bad idea.
You don't know what depends on the system time while executing tests and you don't want to find out the hard way by spending days of debugging into that. Just don't do it.
There is also no way to shadow the time package globally and doing that would not do
anything more you couldn't do with the interface solution. You can write your own time package
which uses the standard library and provides a function to switch to a mock time library for
testing if it is the time object you need to pass around with the interface solution that is bothering you.
The best way to design and test your code would probably be to make as much code stateless as possible.
Split your functionality in testable, stateless parts. Testing these components separately is much easier then. Also, fewer side effects means that it is much easier to make the code run concurrently.
If the methods you need to mock are few, such as Now(), you can make a package variable which can be overwritten by tests:
package foo
import "time"
var now = time.Now
// The rest of your code...which calls now() instead of time.Now()
then in your test file:
package foo
import (
"testing"
"time"
)
var now = func() time.Time { return ... }
// Your tests
I use the bouk/monkey package to replace the time.Now() calls in my code with a fake:
package main
import (
"fmt"
"time"
"github.com/bouk/monkey"
)
func main() {
wayback := time.Date(1974, time.May, 19, 1, 2, 3, 4, time.UTC)
patch := monkey.Patch(time.Now, func() time.Time { return wayback })
defer patch.Unpatch()
fmt.Printf("It is now %s\n", time.Now())
}
This works well in tests to fake out system dependencies and avoids the abused dependency injection (DI) pattern. Production code stays separate from test code and you gain useful control of system dependencies.
Also if you need to just stub time.Now you can inject the dependency as a function, e.g.,
func moonPhase(now func() time.Time) {
if now == nil {
now = time.Now
}
// Use now()...
}
// Then dependent code uses just
moonPhase(nil)
// And tests inject own version
stubNow := func() time.Time { return time.Unix(1515151515, 0) }
moonPhase(stubNow)
Granted, all that is a bit ugly if you come from a dynamic languages background (e.g., Ruby) :(
There are multiple way to mock or stub time.Now() in test code:
Passing an instance of time to the function
func CheckEndOfMonth(now time.Time) {
...
}
Passing a generator to the function
CheckEndOfMonth(now func() time.Time) {
// ...
x := now()
}
Abstract with an interface
type Clock interface {
Now() time.Time
}
type realClock struct {}
func (realClock) Now() time.Time { return time.Now() }
func main() {
CheckEndOfMonth(realClock{})
}
Package level time generator function
type nowFuncT func() time.Time
var nowFunc nowFuncT
func TestCheckEndOfMonth(t *Testing.T) {
nowFunc = func() time.Time {
return time.Now()
}
defer function() {
nowFunc = time.Now
}
// Test your code here
}
Embed time generator in struct
type TimeValidator struct {
// .. your fields
clock func() time.Time
}
func (t TimeValidator) CheckEndOfMonth() {
x := t.now()
// ...
}
func (t TimeValidator) now() time.Time {
if t.clock == nil {
return time.Now() // default implementation which fall back to standard library
}
return t.clock()
}
Each has its own pluses and minuses. The best way is to separate the function that generates the time and the processing part that uses the time.
The post Stubbing Time in golang goes into details about it and there is an example for making function with time dependency to be easily tested.
We can stub time.Now simply by using the Go package undefinedlabs/go-mpatch.
Import the go-mpatch package and put the below code snippet in the code wherever you need to stub time.Now():
mpatch.PatchMethod(time.Now, func() time.Time {
return time.Date(2020, 11, 01, 00, 00, 00, 0, time.UTC)
})
Replace the values of time.Date as per your need.
check out the sample code to check the working of go-mpatch.
go-playground sample
I found a relatively simple solution here. The basic idea is using another function called "nowFunc" to get the time.Now().
In your main, initialize this function to return time.Now(). In your test, initialize this function to return a fixed fake time.
This is the same as Jonathan Hall's answer, but I am adding a concrete example.
Concept:
You can create a global function called CurrentTime to wrap the time.now()
Reassign the CurrentTime function in tests, and make it return the desired value.
File main.go
package main
import (
"fmt"
"time"
)
func main() {
fmt.Printf("This is the current year : %d ", GetCurrentYear())
}
// 'GetCurrentYear' function uses 'CurrentTime' function internally
func GetCurrentYear() int {
return CurrentTime().Year()
}
var CurrentTime = func() time.Time {
return time.Now()
}
File main_test.go
package main
import (
"testing"
"time"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type S struct{}
var _ = Suite(&S{})
func (s *S) TestCurrentYearShouldBeReturnedCorrectly(c *C) {
expectedYear := 2022
curentInstant := time.Date(expectedYear, 12, 01, 00, 00, 00, 0, time.UTC)
// Make 'CurrentTime' return hard-coded time in tests
CurrentTime = func() time.Time {
return curentInstant
}
c.Assert(GetCurrentYear(), Equals, expectedYear)
}
Here is the Go Playground link.
The simple alternative is you can use sqlmock.AnyArg() to pass time.Now() as an argument.
Example
If the query is
[sqlBuilder.Update][2](tableName).Set("last_updated", time.Now()).Where(sq.Eq{"id": id}).ToSql()
and you want to mock this, do
sqlMock.ExpectExec("UPDATE tableName SET last_updated = ? WHERE id = ?").WithArgs(sqlmock.AnyArg())
instead of
sqlMock.ExpectExec("UPDATE tableName SET last_updated = ? WHERE id = ?").WithArgs(time.Now())
You can also use the faketime method used for Go Playground.
It will keep an internal "clock" value which replaces time.Now(), and will instantly return from any call to time.Sleep(), merely increasing the internal counter.
All calls to runtime.write (for example, fmt.Println) will be prefixed with the following header:
\0 \0 P B <8-byte time> <4-byte data length> (big endian)
It was implemented here: https://github.com/golang/go/commit/5ff38e476177ce9e67375bd010bea2e030f2fe19
Using it is as simple as go run -tags=faketime test.go
Example test.go:
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("Test!")
time.Sleep(time.Second * 5)
fmt.Println("Done.")
}
Output:
go run -v -tags=faketime scratch_22.go | hexdump -C
00000000 00 00 50 42 11 74 ef ed ab 18 60 00 00 00 00 06 |..PB.t....`.....|
00000010 54 65 73 74 21 0a 00 00 50 42 11 74 ef ee d5 1e |Test!...PB.t....|
00000020 52 00 00 00 00 06 44 6f 6e 65 2e 0a |R.....Done..|
0000002c
However, I wouldn't recommend using this for actual unit tests, as the change to runtime.write will probably have unintended consequences, breaking a lot of other things.
What works for me is a small struct
package clock
import "time"
type Clock struct {
MockTime time.Time
}
func (c Clock) Now() time.Time {
if c.MockTime.IsZero() {
return time.Now() // use default golang
} else {
return c.MockTime
}
}
Embed the Clock struct in your struct as a dependency, or pass it along as function parameter.

Is there an easy way to stub out time.Now() globally during test?

Part of our code is time-sensitive, and we need to able to reserve something and then release it in 30-60 seconds, etc., which we can just do a time.Sleep(60 * time.Second).
I have just implemented the time interface, and during the test I used a stubbed implementation of the time interface, similar to this golang-nuts discussion.
However, time.Now() is called in multiple sites which means we need to pass a variable around to keep track of how much time we have actually slept.
Is there an alternative way to stub out time.Now() globally? Maybe making a system call to change the system clock?
Can we maybe write our own time package which basically wraps around the time package but allows us to change it?
Our current implementation works well. I am a Go beginner, and I am curious to see if anyone has other ideas.
With implementing a custom interface you are already on the right way. I take it you use the following advise from the golang-nuts thread you've posted:
type Clock interface {
Now() time.Time
After(d time.Duration) <-chan time.Time
}
and provide a concrete implementation
type realClock struct{}
func (realClock) Now() time.Time { return time.Now() }
func (realClock) After(d time.Duration) <-chan time.Time { return time.After(d) }
and a testing implementation.
Original
Changing the system time while making tests (or in general) is a bad idea.
You don't know what depends on the system time while executing tests and you don't want to find out the hard way by spending days of debugging into that. Just don't do it.
There is also no way to shadow the time package globally and doing that would not do
anything more you couldn't do with the interface solution. You can write your own time package
which uses the standard library and provides a function to switch to a mock time library for
testing if it is the time object you need to pass around with the interface solution that is bothering you.
The best way to design and test your code would probably be to make as much code stateless as possible.
Split your functionality in testable, stateless parts. Testing these components separately is much easier then. Also, fewer side effects means that it is much easier to make the code run concurrently.
If the methods you need to mock are few, such as Now(), you can make a package variable which can be overwritten by tests:
package foo
import "time"
var now = time.Now
// The rest of your code...which calls now() instead of time.Now()
then in your test file:
package foo
import (
"testing"
"time"
)
var now = func() time.Time { return ... }
// Your tests
I use the bouk/monkey package to replace the time.Now() calls in my code with a fake:
package main
import (
"fmt"
"time"
"github.com/bouk/monkey"
)
func main() {
wayback := time.Date(1974, time.May, 19, 1, 2, 3, 4, time.UTC)
patch := monkey.Patch(time.Now, func() time.Time { return wayback })
defer patch.Unpatch()
fmt.Printf("It is now %s\n", time.Now())
}
This works well in tests to fake out system dependencies and avoids the abused dependency injection (DI) pattern. Production code stays separate from test code and you gain useful control of system dependencies.
Also if you need to just stub time.Now you can inject the dependency as a function, e.g.,
func moonPhase(now func() time.Time) {
if now == nil {
now = time.Now
}
// Use now()...
}
// Then dependent code uses just
moonPhase(nil)
// And tests inject own version
stubNow := func() time.Time { return time.Unix(1515151515, 0) }
moonPhase(stubNow)
Granted, all that is a bit ugly if you come from a dynamic languages background (e.g., Ruby) :(
There are multiple way to mock or stub time.Now() in test code:
Passing an instance of time to the function
func CheckEndOfMonth(now time.Time) {
...
}
Passing a generator to the function
CheckEndOfMonth(now func() time.Time) {
// ...
x := now()
}
Abstract with an interface
type Clock interface {
Now() time.Time
}
type realClock struct {}
func (realClock) Now() time.Time { return time.Now() }
func main() {
CheckEndOfMonth(realClock{})
}
Package level time generator function
type nowFuncT func() time.Time
var nowFunc nowFuncT
func TestCheckEndOfMonth(t *Testing.T) {
nowFunc = func() time.Time {
return time.Now()
}
defer function() {
nowFunc = time.Now
}
// Test your code here
}
Embed time generator in struct
type TimeValidator struct {
// .. your fields
clock func() time.Time
}
func (t TimeValidator) CheckEndOfMonth() {
x := t.now()
// ...
}
func (t TimeValidator) now() time.Time {
if t.clock == nil {
return time.Now() // default implementation which fall back to standard library
}
return t.clock()
}
Each has its own pluses and minuses. The best way is to separate the function that generates the time and the processing part that uses the time.
The post Stubbing Time in golang goes into details about it and there is an example for making function with time dependency to be easily tested.
We can stub time.Now simply by using the Go package undefinedlabs/go-mpatch.
Import the go-mpatch package and put the below code snippet in the code wherever you need to stub time.Now():
mpatch.PatchMethod(time.Now, func() time.Time {
return time.Date(2020, 11, 01, 00, 00, 00, 0, time.UTC)
})
Replace the values of time.Date as per your need.
check out the sample code to check the working of go-mpatch.
go-playground sample
I found a relatively simple solution here. The basic idea is using another function called "nowFunc" to get the time.Now().
In your main, initialize this function to return time.Now(). In your test, initialize this function to return a fixed fake time.
This is the same as Jonathan Hall's answer, but I am adding a concrete example.
Concept:
You can create a global function called CurrentTime to wrap the time.now()
Reassign the CurrentTime function in tests, and make it return the desired value.
File main.go
package main
import (
"fmt"
"time"
)
func main() {
fmt.Printf("This is the current year : %d ", GetCurrentYear())
}
// 'GetCurrentYear' function uses 'CurrentTime' function internally
func GetCurrentYear() int {
return CurrentTime().Year()
}
var CurrentTime = func() time.Time {
return time.Now()
}
File main_test.go
package main
import (
"testing"
"time"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type S struct{}
var _ = Suite(&S{})
func (s *S) TestCurrentYearShouldBeReturnedCorrectly(c *C) {
expectedYear := 2022
curentInstant := time.Date(expectedYear, 12, 01, 00, 00, 00, 0, time.UTC)
// Make 'CurrentTime' return hard-coded time in tests
CurrentTime = func() time.Time {
return curentInstant
}
c.Assert(GetCurrentYear(), Equals, expectedYear)
}
Here is the Go Playground link.
The simple alternative is you can use sqlmock.AnyArg() to pass time.Now() as an argument.
Example
If the query is
[sqlBuilder.Update][2](tableName).Set("last_updated", time.Now()).Where(sq.Eq{"id": id}).ToSql()
and you want to mock this, do
sqlMock.ExpectExec("UPDATE tableName SET last_updated = ? WHERE id = ?").WithArgs(sqlmock.AnyArg())
instead of
sqlMock.ExpectExec("UPDATE tableName SET last_updated = ? WHERE id = ?").WithArgs(time.Now())
You can also use the faketime method used for Go Playground.
It will keep an internal "clock" value which replaces time.Now(), and will instantly return from any call to time.Sleep(), merely increasing the internal counter.
All calls to runtime.write (for example, fmt.Println) will be prefixed with the following header:
\0 \0 P B <8-byte time> <4-byte data length> (big endian)
It was implemented here: https://github.com/golang/go/commit/5ff38e476177ce9e67375bd010bea2e030f2fe19
Using it is as simple as go run -tags=faketime test.go
Example test.go:
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("Test!")
time.Sleep(time.Second * 5)
fmt.Println("Done.")
}
Output:
go run -v -tags=faketime scratch_22.go | hexdump -C
00000000 00 00 50 42 11 74 ef ed ab 18 60 00 00 00 00 06 |..PB.t....`.....|
00000010 54 65 73 74 21 0a 00 00 50 42 11 74 ef ee d5 1e |Test!...PB.t....|
00000020 52 00 00 00 00 06 44 6f 6e 65 2e 0a |R.....Done..|
0000002c
However, I wouldn't recommend using this for actual unit tests, as the change to runtime.write will probably have unintended consequences, breaking a lot of other things.
What works for me is a small struct
package clock
import "time"
type Clock struct {
MockTime time.Time
}
func (c Clock) Now() time.Time {
if c.MockTime.IsZero() {
return time.Now() // use default golang
} else {
return c.MockTime
}
}
Embed the Clock struct in your struct as a dependency, or pass it along as function parameter.