Hello World程序實例
Go變量實例
Go常量實例
Go for循環語句實例
Go if/else語句實例
Go switch語句實例
Go切片實例
Go範圍實例
Go函數實例
Go函數多個返回值實例
Go可變參數的函數實例
Go閉包(匿名函數)實例
Go函數遞歸實例
Go指針實例
Go指針實例
Go接口實例
Go錯誤實例
Go程序實例
Go通道實例
Go通道緩衝實例
Go通道同步實例
Go通道路線實例
Go Select實例
Go超時(timeouts)實例
Go非阻塞通道操作實例
Go關閉通道實例
Go通道範圍實例
Go計時器實例
Go斷續器實例
Go工作池實例
Go速率限制實例
Go原子計數器實例
Go互斥體實例
Go有狀態的goroutines實例
Go排序實例
Go按自定義函數排序實例
Go panic錯誤處理實例
Go延遲(defer)實例
Go集合函數實例
Go字符串函數實例
Go字符串格式化實例
Go正則表達式實例
Go JSON實例
Go時間日期實例
Go時代(Epoch)實例
Go時間格式化/解析實例
Go隨機數實例
Go數字解析實例
Go URL解析實例
Go SHA1哈希實例
Go Base64編碼實例
Go讀取文件實例
Go寫文件實例
Go行過濾器實例
Go命令行參數實例
Go命令行標誌實例
Go環境變量實例
Go執行過程實例
Go信號實例
Go退出程序實例
Go時間日期實例
Go編程爲時間和持續時間提供廣泛的支持; 這裏有些例子。我們將從獲取當前時間開始。也可以通過提供年,月,日等來構建時間結構。時間總是與位置(即時區)相關聯。
具體的每個函數,可參考示例中的代碼 -
所有的示例代碼,都放在
F:\worksp\golang
目錄下。安裝Go編程環境請參考:http://www.yiibai.com/go/go\_environment.html
json.go
的完整代碼如下所示 -
package main
import "fmt"
import "time"
func main() {
p := fmt.Println
// We'll start by getting the current time.
now := time.Now()
p(now)
// You can build a `time` struct by providing the
// year, month, day, etc. Times are always associated
// with a `Location`, i.e. time zone.
then := time.Date(
2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
p(then)
// You can extract the various components of the time
// value as expected.
p(then.Year())
p(then.Month())
p(then.Day())
p(then.Hour())
p(then.Minute())
p(then.Second())
p(then.Nanosecond())
p(then.Location())
// The Monday-Sunday `Weekday` is also available.
p(then.Weekday())
// These methods compare two times, testing if the
// first occurs before, after, or at the same time
// as the second, respectively.
p(then.Before(now))
p(then.After(now))
p(then.Equal(now))
// The `Sub` methods returns a `Duration` representing
// the interval between two times.
diff := now.Sub(then)
p(diff)
// We can compute the length of the duration in
// various units.
p(diff.Hours())
p(diff.Minutes())
p(diff.Seconds())
p(diff.Nanoseconds())
// You can use `Add` to advance a time by a given
// duration, or with a `-` to move backwards by a
// duration.
p(then.Add(diff))
p(then.Add(-diff))
}
執行上面代碼,將得到以下輸出結果 -
F:\worksp\golang>go run time.go
2017-01-21 16:59:10.8307711 +0800 CST
2009-11-17 20:34:58.651387237 +0000 UTC
2009
November
17
20
34
58
651387237
UTC
Tuesday
true
false
false
62916h24m12.179383863s
62916.403383162185
3.774984202989731e+06
2.2649905217938387e+08
226499052179383863
2017-01-21 08:59:10.8307711 +0000 UTC
2002-09-14 08:10:46.472003374 +0000 UTC