使用一個數值表示時間中的“秒”值,然后使用 resolveTime() 函數將傳入的秒數轉換為天、小時和分鍾等時間單位。
package main import "fmt" /* 時間常量 */ const ( //定義每分鍾的秒數 SecondsPerMinute = 60 //定義每小時的秒數 SecondsPerHour = SecondsPerMinute * 60 //定義每天的秒數 SecondsPerDay = SecondsPerHour * 24 ) /* 時間轉換函數 */ func resolveTime(seconds int) (day int, hour int, minute int) { //每分鍾秒數 minute = seconds / SecondsPerMinute //每小時秒數 hour = seconds / SecondsPerHour //每天秒數 day = seconds / SecondsPerDay return } func main() { //打印返回參數 fmt.Println(resolveTime(1000)) //只打印小時和分鍾 _, hour, minute := resolveTime(1800) fmt.Println(hour, minute) //只打印天 day, _, _ := resolveTime(90000) fmt.Println(day) }
程序輸出:
0 0 16 0 30 1