Open In App

time.Time.AppendFormat() Function in Golang With Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// Golang program to illustrate the usage of
// Time.AppendFormat() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Declaring Time
    Time := time.Date(2019, time.January, 
                1, 3, 0, 0, 0, time.UTC)
  
    // Defining byte of slice
    b := []byte("The time is: ")
  
    // Calling AppendFormat method with
    // its parameter
    b = Time.AppendFormat(b, "03:00PM")
  
    // Prints buffer
    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:




// Golang program to illustrate the usage of
// Time.AppendFormat() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Declaring Time
    Time := time.Date(2019, time.January, 1, 
                      3, 0, 0, 0, time.UTC)
  
    // Defining byte of slice
    b := []byte("The time is: ")
  
    // Calling AppendFormat method with
    // its parameter
    b = Time.AppendFormat(b, time.Kitchen)
  
    // Prints string
    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
Previous
Next
Share your thoughts in the comments
Similar Reads