time.Time.AppendFormat() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.AppendFormat() function in Go language is like Format() method but this method also appends the stated textual representation to the stated “b”. 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) AppendFormat(b []byte, layout string) []byte
Here, “t” is the stated time, “b” is the byte of slice and layout holds a string type.
Return value: It appends the textual representation to the “b” and then returns the buffer that is extended.
Example 1:
package main
import "fmt"
import "time"
func main() {
Time := time .Date(2019, time .January,
1, 3, 0, 0, 0, time .UTC)
b := []byte( "The time is: " )
b = Time.AppendFormat(b, "03:00PM" )
fmt.Println(b)
}
|
Output:
[84 104 101 32 116 105 109 101 32 105 115 58 32 48 51 58 48 48 65 77]
Here, textual representation of time is appended to the “b” then its returned as a buffer.
Example 2:
package main
import "fmt"
import "time"
func main() {
Time := time .Date(2019, time .January, 1,
3, 0, 0, 0, time .UTC)
b := []byte( "The time is: " )
b = Time.AppendFormat(b, time .Kitchen)
fmt.Println(string(b))
}
|
Output:
The time is: 3:00AM
Here, a predefined layout is used that is, time.Kitchen which is used in Time.Format. And in the above output a string is returned instead of buffer as string() method is used here to convert a buffer into the string.
Last Updated :
19 Apr, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...