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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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))
}

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.