- time模塊
fmt.Println(time.Now()) //當前時間 fmt.Println(time.Now().Unix()) // 十位數時間戳 fmt.Println(time.Now().Format("2006-01-02 15:04:05")) // 十位數時間戳 fmt.Println(time.Unix(1389058332, 0).Format("2006-01-02 15:04:05")) // 時間戳轉格式化時間 fmt.Println(time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local).Unix()) //格式化時間轉時間戳 t := time.Now() y, m, d := t.Date() fmt.Println(y, m, d) // 2020 February 27 today := time.Now().Format("2006-01-02") fmt.Println(today) datetime := time.Now().Format("2006-01-02 15:04:05") fmt.Println(datetime) t2 := time.Unix(1389058332, 0) delter := t.Sub(t2) // 返回時間差, s fmt.Println(delter)
// time.Tick函數返回一個channel, 每隔固定的時間想chnnel發送一個信號
for range time.Tick(time.Second * 5){
fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
- string()
func main() { var a = []byte(time.Now().String()) fmt.Println(a) //[50 48 50 48 45 48 50 45 50 55 32 49 50 58 48 55 58 51] ascii碼值切片,或者 fmt.Println(reflect.TypeOf(a)) // []uint8 fmt.Println(string(a)) // 將uint8切片還原爲字符串 }
- error模塊的實現方法
// because the former will succeed if err wraps an *os.PathError. package errors // New returns an error that formats as the given text. // Each call to New returns a distinct error value even if the text is identical. func New(text string) error { return &errorString{text} } // errorString is a trivial implementation of error. type errorString struct { s string } func (e *errorString) Error() string { return e.s }