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:
// Golang program to illustrate the usage of // Time.GobEncode() function // Including main package package main // Importing fmt and time import "fmt"
import "time"
// Calling main func main() { // Defining t for GobEncode method
t := time .Date(2018, 2, 1, 14, 30, 0, 0, time .UTC)
// Calling GobEncode() method
encoding, error := t.GobEncode()
// Prints receiver's encoding
fmt.Printf( "Receiver's encoding: %v\n" , encoding)
// Prints error
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:
// Golang program to illustrate the usage of // Time.GobEncode() function // Including main package package main // Importing fmt and time import "fmt"
import "time"
// Calling main func main() { // Defining t for GobEncode method
t := time .Date(2019, 37, 43, 76, 90, 88, 5453, time .UTC)
// Calling GobEncode() method
encoding, error := t.GobEncode()
// Prints receiver's encoding
fmt.Printf( "Receiver's encoding: %v\n" , encoding)
// Prints error
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.