package main
import "fmt"
import "os"
type point struct {
x, y int
}
func main() {
//Go 为常规 Go 值的格式化设计提供了多种打印方式。例如,这里打印了 point 结构体的一个实例。
p := point{1, 2}
fmt.Printf("%v\n", p) // {1 2}
//如果值是一个结构体,%+v 的格式化输出内容将包括结构体的字段名。
fmt.Printf("%+v\n", p) // {x:1 y:2}
//%#v 形式则输出这个值的 Go 语法表示。例如,值的运行源代码片段。
fmt.Printf("%#v\n", p) // main.point{x:1, y:2}
//需要打印值的类型,使用 %T。
fmt.Printf("%T\n", p) // main.point
//格式化布尔值是简单的。
fmt.Printf("%t\n", true)
//格式化整形数有多种方式,使用 %d进行标准的十进制格式化。
fmt.Printf("%d\n", 123)
//这个输出二进制表示形式。
fmt.Printf("%b\n", 14)
这个输出给定整数的对应字符。
fmt.Printf("%c\n", 33)
%x 提供十六进制编码。
fmt.Printf("%x\n", 456)
//对于浮点型同样有很多的格式化选项。使用 %f 进行最基本的十进制格式化。
fmt.Printf("%f\n", 78.9)
//%e 和 %E 将浮点型格式化为(稍微有一点不同的)科学技科学记数法表示形式。
fmt.Printf("%e\n", 123400000.0)
fmt.Printf("%E\n", 123400000.0)
//使用 %s 进行基本的字符串输出。
fmt.Printf("%s\n", "\"string\"")
//像 Go 源代码中那样带有双引号的输出,使用 %q。
fmt.Printf("%q\n", "\"string\"")
//和上面的整形数一样,%x 输出使用 base-16 编码的字符串,每个字节使用 2 个字符表示。
fmt.Printf("%x\n", "hex this")
//要输出一个指针的值,使用 %p。
fmt.Printf("%p\n", &p)
//当输出数字的时候,你将经常想要控制输出结果的宽度和精度,可以使用在 % 后面使用数字来控制输出宽度。默认结果使用右对齐并且通过空格来填充空白部分。
fmt.Printf("|%6d|%6d|\n", 12, 345)
//你也可以指定浮点型的输出宽度,同时也可以通过 宽度.精度 的语法来指定输出的精度。
fmt.Printf("|%6.2f|%6.2f|\n", 1.2, 3.45)
//要最对齐,使用 - 标志。
fmt.Printf("|%-6.2f|%-6.2f|\n", 1.2, 3.45)
//你也许也想控制字符串输出时的宽度,特别是要确保他们在类表格输出时的对齐。这是基本的右对齐宽度表示。
fmt.Printf("|%6s|%6s|\n", "foo", "b")
//要左对齐,和数字一样,使用 - 标志。
fmt.Printf("|%-6s|%-6s|\n", "foo", "b")
//到目前为止,我们已经看过 Printf了,它通过 os.Stdout输出格式化的字符串。Sprintf 则格式化并返回一个字符串而不带任何输出。
s := fmt.Sprintf("a %s", "string")
fmt.Println(s)
//你可以使用 Fprintf 来格式化并输出到 io.Writers而不是 os.Stdout。
fmt.Fprintf(os.Stderr, "an %s\n", "error")
}当前位置:网站首页>golang fmt.printf()(转)
golang fmt.printf()(转)
2022-07-04 12:49:00 【Lucky小黄人】
边栏推荐
- 忠诚协议是否具有法律效力
- Fs7867s is a voltage detection chip used for power supply voltage monitoring of digital system
- Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
- C basic supplement
- 分布式BASE理论
- C语言课程设计题
- WPF double slider control and forced capture of mouse event focus
- C language programming topic reference
- 30: Chapter 3: develop Passport Service: 13: develop [change / improve user information, interface]; (use * * * Bo class to accept parameters, and use parameter verification)
- Fs4056 800mA charging IC domestic fast charging power IC
猜你喜欢

When MDK uses precompiler in header file, ifdef is invalid

高质量软件架构的唯一核心指标

好博医疗冲刺科创板:年营收2.6亿 万永钢和沈智群为实控人
![[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source](/img/2c/b1d6277c1b23a6a77f90d5b2874759.png)
[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source

2022kdd pre lecture | 11 first-class scholars take you to unlock excellent papers in advance

How to choose a technology stack for web applications in 2022

Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)

. Net delay queue

Install Trinity and solve error reporting

华昊中天冲刺科创板:年亏2.8亿拟募资15亿 贝达药业是股东
随机推荐
Redis - how to install redis and configuration (how to quickly install redis on ubuntu18.04 and centos7.6 Linux systems)
JVM series - stack and heap, method area day1-2
Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc
2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
博士申请 | 西湖大学学习与推理系统实验室招收博后/博士/研究实习等
Introduction to reverse debugging PE structure resource table 07/07
The old-fashioned synchronized lock optimization will make it clear to you at once!
Distributed base theory
unity不识别rider的其中一种解决方法
高质量软件架构的唯一核心指标
2022G3锅炉水处理考试题模拟考试题库及模拟考试
Don't turn down, three sentences to clarify the origin of cross domain resource request errors
MySQL 45 lecture - learn the actual combat notes of MySQL in Geek time 45 lecture - 06 | global lock and table lock_ Why are there so many obstacles in adding a field to the table
XML入门一
Scripy framework learning
ViewBinding和DataBinding的理解和区别
美国土安全部部长警告移民“不要踏上危险的旅程”
程序员转方向
基于STM32+华为云IOT设计的酒驾监控系统
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example