zoukankan      html  css  js  c++  java
  • [Go]fmt Sprintf的格式占位符%

    普通占位符

    占位符 说明 举例 输出
    %v 相应值的默认格式。 Printf("%v", people) {zhangsan}
    %+v 打印结构体时,会添加字段名 Printf("%+v", people) {Name:zhangsan}
    %#v 相应值的Go语法表示 Printf("#v", people) main.Human{Name:"zhangsan"}
    %T 相应值的类型的Go语法表示 Printf("%T", people) main.Human
    %% 字面上的百分号,并非值的占位符 Printf("%%") %

    整数占位符

    占位符 说明 举例 输出
    %b 二进制表示 Printf("%b", 5) 101
    %c 相应Unicode码点所表示的字符 Printf("%c", 0x4E2D) 
    %d 十进制表示 Printf("%d", 0x12) 18
    %o 八进制表示 Printf("%d", 10) 12
    %q 单引号围绕的字符字面值,由Go语法安全地转义 Printf("%q", 0x4E2D) '中'
    %x 十六进制表示,字母形式为小写 a-f Printf("%x", 13) d
    %X 十六进制表示,字母形式为大写 A-F  Printf("%x", 13) D
    %U Unicode格式:U+1234,等同于 "U+%04X" Printf("%U", 0x4E2D) U+4E2D

    p := point{1, 2}
        fmt.Printf("%v
    ", p) //{1,2}
        fmt.Printf("%+v
    ", p) //{x:1 y:2}
        fmt.Printf("%#v
    ", p) //main.point{x:1, y:2}
        fmt.Printf("%T
    ", p) // main.point
        fmt.Printf("%t
    ", true) //true
        fmt.Printf("%d
    ", 123) //123
        fmt.Printf("%b
    ", 14) //1110
        fmt.Printf("%c
    ", 33) //!
        fmt.Printf("%x
    ", 456) //1c8
        fmt.Printf("%f
    ", 78.9) //78.900000
        fmt.Printf("%e
    ", 123400000.0) //1.234000e+08
        fmt.Printf("%E
    ", 123400000.0) //1.234000E+08
        fmt.Printf("%s
    ", ""string"") //"string"
        fmt.Printf("%q
    ", ""string"")//""string""
        fmt.Printf("%x
    ", "hex this") //6865782074686973
        fmt.Printf("%p
    ", &p) //0xc0000b4010
        fmt.Printf("|%6d|%6d|
    ", 12, 345) //|    12|   345|
        fmt.Printf("|%6.2f|%6.2f|
    ", 1.2, 3.45) //|  1.20|  3.45|
        fmt.Printf("|%-6.2f|%-6.2f|
    ", 1.2, 3.45) //|1.20  |3.45  |
        fmt.Printf("|%6s|%6s|
    ", "foo", "b") //|   foo|     b|
        fmt.Printf("|%-6s|%-6s|
    ", "foo", "b") //|foo   |b     |
        s := fmt.Sprintf("a %s", "string") 
        fmt.Println(s) //a string
        fmt.Fprintf(os.Stderr, "an %s
    ", "error")//an error

    开源作品

    GO-FLY,一套可私有化部署的免费开源客服系统,安装过程不超过五分钟(超过你打我 !),基于Golang开发,二进制文件可直接使用无需搭开发环境,下载zip解压即可,仅依赖MySQL数据库,是一个开箱即用的网页在线客服系统,致力于帮助广大开发者/中小站长快速整合私有客服功能
    github地址:go-fly
    官网地址:https://gofly.sopans.com

    赞赏作者

    微信交流

  • 相关阅读:
    [转发]深入理解git,从研究git目录开始
    iOS系统网络抓包方法
    charles抓包工具
    iOS多线程中performSelector: 和dispatch_time的不同
    IOS Core Animation Advanced Techniques的学习笔记(五)
    IOS Core Animation Advanced Techniques的学习笔记(四)
    IOS Core Animation Advanced Techniques的学习笔记(三)
    IOS Core Animation Advanced Techniques的学习笔记(二)
    IOS Core Animation Advanced Techniques的学习笔记(一)
    VirtualBox复制CentOS后提示Device eth0 does not seem to be present的解决方法
  • 原文地址:https://www.cnblogs.com/taoshihan/p/14621845.html
Copyright © 2011-2022 走看看