golang fmt用法举例

下标与参数的对应

例子如下:

package main



import (
        "fmt"

)


func main() {
        num := 10

        fmt.Printf("num:%d, num:%[1]o, num:%[1]x
", num)

}


output:

num:10, num:12, num:a

分别输出整数的10进制, 8进制, 16进制形式。
第二个,第三个输出,均使用第一个参数,引用方式"[n]"。

空格的输出

在格式化字符串中可以使用“*”,输出任意多个空格。
例如:

%d
%
s

example如下:

package main



import (
        "fmt"

)


func main() {


        fmt.Printf("echo:%*s,end
", 10, "Hello")
}


output:

echo: Hello,end

原文地址:https://www.cnblogs.com/lanyangsh/p/8321251.html