time.Time.GobEncode() Function in Golang with Examples
Last Updated :
28 Apr, 2020
In Go language, time packages supplies functionality for determining as well as viewing time. The GobEncode() function in Go language is used to implement the gob.GobEncoder interface. Moreover, this function is defined under the time package. Here, you need to import the “time” package in order to use these functions.
Syntax:
func (t Time) GobEncode() ([]byte, error)
Here, “t” is the stated time and two values of type “byte” and “error” are returned as output in this method.
Return value: It returns a byte slice that represents the receiver’s encoding so that it can be transmitted to a GobDecoder and also returns an error occurred but if there are no errors then “nil” is returned.
Example 1:
package main
import "fmt"
import "time"
func main() {
t := time .Date(2018, 2, 1, 14, 30, 0, 0, time .UTC)
encoding, error := t.GobEncode()
fmt.Printf( "Receiver's encoding: %v\n" , encoding)
fmt.Printf( "Error occurred: %v\n" , error)
}
|
Output:
Receiver's encoding: [1 0 0 0 14 210 5 27 104 0 0 0 0 255 255]
Error occurred: nil
Example 2:
package main
import "fmt"
import "time"
func main() {
t := time .Date(2019, 37, 43, 76, 90, 88, 5453, time .UTC)
encoding, error := t.GobEncode()
fmt.Printf( "Receiver's encoding: %v\n" , encoding)
fmt.Printf( "Error occurred: %v\n" , error)
}
|
Output:
Receiver's encoding: [1 0 0 0 14 217 157 49 176 0 0 21 77 255 255]
Error occurred: nil
Here, the “t” stated in the above code have values that are outside usual range but they are normalized while conversion.
Share your thoughts in the comments
Please Login to comment...