xxx go内置函数

  • 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
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章