當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang time.Time.AppendFormat()用法及代碼示例

在Go語言中,時間包提供了確定和查看時間的函數。 Go語言中的Time.AppendFormat()函數類似於Format()方法,但該方法還將聲明的文本表示形式附加到聲明的“b”。此外,此函數在時間包下定義。在這裏,您需要導入“time”包才能使用這些函數。

用法:

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

在這裏,“t”是指定的時間,“b”是切片的字節,布局保留字符串類型。

返回值:它將文本表示形式附加到“b”,然後返回擴展的緩衝區。

範例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) 
}

輸出:

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

在此,時間的文本表示形式將附加到“b”上,然後作為緩衝區返回。

範例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)) 
}

輸出:

The time is:3:00AM

在這裏,使用預定義的布局,即time.Kitchen,它在Time.Format中使用。在上麵的輸出中,返回一個字符串而不是緩衝區,因為此處使用string()方法將緩衝區轉換為字符串。




相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 time.Time.AppendFormat() Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。