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小黄人】
边栏推荐
- .NET 使用 redis
- ASP.NET Core入门一
- . Net using redis
- PostgreSQL 9.1 soaring Road
- C语言程序设计选题参考
- 上汽大通MAXUS正式发布全新品牌“MIFA”,旗舰产品MIFA 9正式亮相!
- When MDK uses precompiler in header file, ifdef is invalid
- SQL语言
- The old-fashioned synchronized lock optimization will make it clear to you at once!
- Dgraph: large scale dynamic graph dataset
猜你喜欢

Node の MongoDB安装

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

Redis —— How To Install Redis And Configuration(如何快速在 Ubuntu18.04 与 CentOS7.6 Linux 系统上安装 Redis)

上汽大通MAXUS正式发布全新品牌“MIFA”,旗舰产品MIFA 9正式亮相!

The only core indicator of high-quality software architecture

如何在 2022 年为 Web 应用程序选择技术堆栈

【R语言数据科学】:交叉验证再回首

Summary of recent days (non-technical article)

Introduction to reverse debugging PE structure resource table 07/07

Install Trinity and solve error reporting
随机推荐
C language programming topic reference
Fisher信息量检测对抗样本代码详解
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
c#数组补充
Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)
Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc
C语言中学生成绩管理系统
Introduction to XML I
Worried about "cutting off gas", Germany is revising the energy security law
Alibaba cloud award winning experience: build a highly available system with polardb-x
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
C語言宿舍管理查詢軟件
Optional values and functions of the itemized contenttype parameter in the request header
2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
Dgraph: large scale dynamic graph dataset
英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
C language Dormitory Management Query Software
Database lock table? Don't panic, this article teaches you how to solve it
苹果5G芯片研发失败:继续依赖高通,还要担心被起诉?
免费、好用、强大的轻量级笔记软件评测:Drafts、Apple 备忘录、Flomo、Keep、FlowUs、Agenda、SideNote、Workflowy