我想在 go 中以某種格式返回當(dāng)前時(shí)間,我在格式化時(shí)間方面沒(méi)有問(wèn)題,但是當(dāng)在 func 中將它作為字符串返回時(shí),我被卡住了:package mainimport ( "fmt" "time")func getCurrentTime()string{ t := time.Now().Local() return fmt.Sprintf("%s", t.Format("2006-01-02 15:04:05 +0800"))}func main() { fmt.Println("current Time is:",getCurrentTime) t := time.Now().Local() fmt.Println("current Time is:", t.Format("2006-01-02 15:04:05 +0800"))}輸出是:current Time is: 0x400c00current Time is: 2015-01-16 12:45:33 +0800代替current Time is: 2015-01-16 12:45:33 +0800current Time is: 2015-01-16 12:45:33 +0800這是我所期望的。
1 回答

臨摹微笑
TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊
在你的main
函數(shù)中,你應(yīng)該使用getCurrentTime()
而不是getCurrentTime
. 像這樣:
fmt.Println("current Time is:", getCurrentTime())
當(dāng)您將函數(shù)名作為參數(shù)傳遞時(shí),您并不是在調(diào)用它,而是實(shí)際打印了函數(shù)的地址。
- 1 回答
- 0 關(guān)注
- 190 瀏覽
添加回答
舉報(bào)
0/150
提交
取消