Golang之时间格式化,计时器

地鼠敲下一堆代码,记录着当天的时间

package main

import (
    "fmt"
    "time"
)

func getTime() {
    now := time.Now()
    timeStr := now.Format("2006/01/02 15:04:05")
    fmt.Printf("time:%s\n", timeStr)
}

func testFormat2() {
    now := time.Now()
    timeStr := fmt.Sprintf("%02d/%02d/%02d %02d:%02d:%02d\n",
        now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
    fmt.Printf("time:%s\n", timeStr)
}

func testCost() {
    start := time.Now().UnixNano()
    for i := 0; i < 10; i++ {
        time.Sleep(time.Millisecond)
    }
    end := time.Now().UnixNano()
    cost := (end - start) / 1000
    fmt.Printf("code cost:%d us\n", cost)
}

func main() {
    //getTime()
    //testFormat2()
    testCost()
}

 

posted @ 2018-02-09 16:02  py鱼  阅读(623)  评论(0编辑  收藏  举报
点我回主页