Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 使用fmt.Printf为数字添加零填充_Go_Printf - Fatal编程技术网

Go 使用fmt.Printf为数字添加零填充

Go 使用fmt.Printf为数字添加零填充,go,printf,Go,Printf,我有一个关于函数fmt.Printf的问题; 为什么当我使用这个函数时,程序会忽略第一个零 package main import ( "fmt" "time" ) func main() { now := time.Now() year, month, day := now.Date() hour, min, sec := now.Clock() fmt.Printf("%d-%s-%d_%d:%d:%d\n", year, month, day, ho

我有一个关于函数fmt.Printf的问题; 为什么当我使用这个函数时,程序会忽略第一个零

package main

import (
   "fmt"
   "time"
)

func main() {
   now := time.Now()
   year, month, day := now.Date()
   hour, min, sec := now.Clock()
   fmt.Printf("%d-%s-%d_%d:%d:%d\n", year, month, day, hour, min, sec)
}
返回时间:2017年4月26日10时03分2秒


有人能帮我吗?

只需将
%d
替换为
%02d
,即可

fmt.Printf("%d-%s-%02d_%02d:%02d:%02d\n", year, month, day, hour, min, sec)

只需将
%d
替换为
%02d
,即

fmt.Printf("%d-%s-%02d_%02d:%02d:%02d\n", year, month, day, hour, min, sec)