Golang time.Time.AppendFormat()函数及示例

Golang time.Time.AppendFormat()函数及示例

在Go语言中,time包提供了确定和查看时间的功能。Golang中的 Time.AppendFormat() 函数类似于Format()方法,但该方法还将所述文本表示附加到所述“b”中。此外,此功能在time包中定义。在这里,您需要导入“time”包以使用这些功能。

Syntax:

func (t Time) AppendFormat(b []byte, layout string) []byte

在这里,“t”是指定的时间,“b”是字节片,layout是字符串类型。

返回值:

它将文本表示附加到“b”,然后返回已扩展的缓冲区。

示例1:

//Golang程序,演示Time.AppendFormat()函数的用法

//包含主包
package main

//导入fmt和time
import "fmt"
import "time"

//调用main
func main() {

//声明时间
时间:= time.Date(2019, time.January, 1, 3, 0, 0, 0, time.UTC)

//定义字节片
b:= []byte("时间为:")

//使用其参数调用AppendFormat方法
b= 时间.AppendFormat(b, "03:00PM")

//打印缓冲区
fmt.Println(b)
} 

输出:

[84 104 101 32 116 105 109 101 32 105 115 58 32 48 51 58 48 48 65 77]

这里,时间的文本表示被附加到“b”中,然后作为缓冲区返回。

示例2:

//Golang程序,演示Time.AppendFormat()函数的用法

//包含主包
package main

//导入fmt和time
import "fmt"
import "time"

//调用main
func main() {

//声明时间
时间:= time.Date(2019, time.January, 1, 3, 0, 0, 0, time.UTC)

//定义字节片
b:= []byte("时间为:")

//使用其参数调用AppendFormat方法
b= 时间.AppendFormat(b, time.Kitchen)

//打印字符串
fmt.Println(string(b))
} 

输出:

时间为:3:00AM

这里使用了一个预定义的布局,即time.Kitchen,用于Time.Format。在上面的输出中,返回了一个字符串,而不是缓冲区,因为在这里使用string()方法将缓冲区转换为字符串。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程