iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 17
2
自我挑戰組

Go劍復國-30天導入Golang系列 第 17

Golang 常用函式庫-2

今天這篇,也一樣是介紹一些,golang 常用的 stdlib ,來幫助大家可以更快更方便的,使用 golang 做一些常見的應用。

time

golang的時間單位如下

const (
        Nanosecond  Duration = 1
        Microsecond          = 1000 * Nanosecond
        Millisecond          = 1000 * Microsecond
        Second               = 1000 * Millisecond
        Minute               = 60 * Second
        Hour                 = 60 * Minute
)

有了上面的單位換算,接下來我們來看下面的使用範例

package main

import (
	"fmt"
	"time"
)

func main() {
	//unix time
	fmt.Println("unix: ", time.Now().Unix())

	//取到 nano second
	fmt.Println("unix: ", time.Now().UnixNano())

	//format成 正常格式化後的時間
	fmt.Println("datetime: ", time.Now().Format("2006/01/02 15:04:05"))

	//after
	time.AfterFunc(3*time.Second, func() {
		fmt.Println("hello world")
	})

	//這邊 Sleep 5s 是為了讓上面的 AfterFunc 會執行,不然就像前面章節有講到的 只要 main thread 結束,任何 sub thread 都會跟著一起結束
	time.Sleep(5 * time.Second)
	fmt.Println("end")
}

https://play.golang.org/p/oryLc7H6HYR

在這邊要特別說明一下,這個 Format 語法,為什麼沒有像其他語言一樣用 %d、%M....,之類的符號呢?

為什麼會這樣呢? 難道是 2006-01-02 15:04:05 發生了什麼事情? 先看一下他的src

const (
        _                        = iota
        stdLongMonth             = iota + stdNeedDate  // "January"
        stdMonth                                       // "Jan"
        stdNumMonth                                    // "1"
        stdZeroMonth                                   // "01"
        stdLongWeekDay                                 // "Monday"
        stdWeekDay                                     // "Mon"
        stdDay                                         // "2"
        stdUnderDay                                    // "_2"
        stdZeroDay                                     // "02"
        stdHour                  = iota + stdNeedClock // "15"
        stdHour12                                      // "3"
        stdZeroHour12                                  // "03"
        stdMinute                                      // "4"
        stdZeroMinute                                  // "04"
        stdSecond                                      // "5"
        stdZeroSecond                                  // "05"
        stdLongYear              = iota + stdNeedDate  // "2006"
        stdYear                                        // "06"
        stdPM                    = iota + stdNeedClock // "PM"
        stdpm                                          // "pm"
        stdTZ                    = iota                // "MST"
        stdISO8601TZ                                   // "Z0700"  // prints Z for UTC
        stdISO8601SecondsTZ                            // "Z070000"
        stdISO8601ColonTZ                              // "Z07:00" // prints Z for UTC
        stdISO8601ColonSecondsTZ                       // "Z07:00:00"
        stdNumTZ                                       // "-0700"  // always numeric
        stdNumSecondsTz                                // "-070000"
        stdNumShortTZ                                  // "-07"    // always numeric
        stdNumColonTZ                                  // "-07:00" // always numeric
        stdNumColonSecondsTZ                           // "-07:00:00"
        stdFracSecond0                                 // ".0", ".00", ... , trailing zeros included
        stdFracSecond9                                 // ".9", ".99", ..., trailing zeros omitted

        stdNeedDate  = 1 << 8             // need month, day, year
        stdNeedClock = 2 << 8             // need hour, minute, second
        stdArgShift  = 16                 // extra argument in high bits, above low stdArgShift
        stdMask      = 1<<stdArgShift - 1 // mask out argument
)

裡面就有講到他的設定,就是照這幾個數字去 parse,記憶口訣是基於
01/02 03:04:05PM '06 -0700
有沒有注意到?剛好是 1234567 的順序,很難講這個有別於其他語言的設定,到底是好還是不好,不過要花點時間去適應就是了。

log

我想寫任何服務、套件...,都免不了一定要有 log ,golang 在這邊有提供最基礎的 log lib

package main

import (
	"log"
)

func main() {
	//最基礎的 印出 hello world
	log.Println("[standard] hello world")

	//設定輸出的格式
	log.SetFlags(log.Ltime)
	log.Println("[setflag]  hello world")

	//設定前綴
	log.SetPrefix("[ithome]")
	log.Println("[prefix & setflag] hello world")

}

https://play.golang.org/p/VRge9_XELte

errors

errors 就是提供一個,可以快速回應一個 error interface 的 error,當然如果有特殊需求,可以像前面 interface 介紹的去實作一個 error interface。

package main

import (
	"errors"
	"fmt"
)

func main() {
	err := errors.New("errorrrrrrr")
	if err != nil {
		fmt.Print(err)
	}
}

https://play.golang.org/p/7bLmuTOyW1n


上一篇
Golang 常用函式庫-1
下一篇
Golang 常用函式庫-3
系列文
Go劍復國-30天導入Golang30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言