Below is a code snippet - I am confused as to how to assign to variables within my nested struct ("myTime") which I am using for JSON decoding. (I have some Unix timestamps in a JSON file and am hoping to learn how to decode them.)
This throws the following error:
main.go:15: cannot use time.Unix(a, 0) (type time.Time) as type *myTime in assignment
main.go:25: t.String undefined (type myTime has no field or method String)
I'm not sure exactly how to go about understanding the issue, so any explanation or a pointer to specific documentation would help greatly!
package main
import (
"encoding/binary"
"encoding/json"
"fmt"
"log"
"time"
)
type myTime time.Time
func (t *myTime) UnmarshalJSON(buf []byte) error {
a, _ := binary.Varint(buf)
t = time.Unix(a, 0)
return nil
}
func main() {
var t myTime
if err := json.Unmarshal([]byte("123123123.123123"), &t); err != nil {
log.Fatal(err)
}
fmt.Printf("result: %f
", t.String())
}