Standard library / time

time.Time.MarshalBinary()

Encode a Time into binary form; check errors and use UnmarshalBinary to decode. Monotonic data and the named location rules are not serialized. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (t Time) MarshalBinary() ([]byte, error)

Return Value

([]byte, error)

Encode a Time into binary form; check errors and use UnmarshalBinary to decode.

Working Example

Example Snippetgo
package main
import (  "fmt"  "time")
func fixed() time.Time { return time.Date(2024, 2, 29, 12, 34, 56, 123000000, time.UTC) }func must(err error) {  if err != nil {    panic(err)  }}func main() {  data, err := fixed().MarshalBinary()  must(err)  var t time.Time  must(t.UnmarshalBinary(data))  fmt.Println(t.Equal(fixed()))}
Go Reference