当前位置:网站首页>go笔记(3)Go语言fmt包的用法
go笔记(3)Go语言fmt包的用法
2022-07-04 19:03:00 【fiveym】
介绍
fmt是一个用于输入输出常用的库
在fmt包,有关格式化输入输出的方法就有两大类:scan和print
分别在scan.go和print.go文件中
print:输出函数
print 系列主要用于输出,主要包含三个方法:
print:直接输出内容, 不会换行 ,不能格式化输出。
printf: 按照指定格式化文本输出内容。
println:能够在输出内容后面 加上换行符 。
package main
import "fmt"
func main() {
// 一次输入多个值的时候 Println 中间有空格,能自动换行
fmt.Println("Hello", "Println")
// 一次输入多个值的时候 Print 没有中间空格,Print 不会自动换行;
fmt.Print("Hello", "Print")
// Printf 是格式化输出,在很多场景下比 Println 更方便,Printf也不会换行
fmt.Printf("Hello Printf %s", "AAA")
}
// Hello Println
// HelloPrintHello Printf AA
print.go文件中定义了9个函数
这9个函数,按照两个维度功能可以按照两个维度来划分
//如果把 Print 理解为核心关键字,那么后面跟的后缀有" f "和" ln "以及"",着重的是输出内容的最终结果;如果后缀是" f ", 则指定了format 如果后缀是" ln ", 则有换行符。
Println、Fprintln、Sprintln 输出内容时会加上换行符;
Print、Fprint、Sprint 输出内容时不加上换行符;
Printf、Fprintf、Sprintf 按照指定格式化文本输出内容。
//如果把 Print 理解为核心关键字,那么前面的前缀有" F "和" S "以及"",着重的是输出内容的目标(终端);如果前缀是" F ", 则指定了 io.Writer 如果前缀是" S ", 则是输出到字符串
Print、Printf、Println 输出内容到标准输出os.Stdout;
Fprint、Fprintf、Fprintln 输出内容到指定的io.Writer;
Sprint、Sprintf、Sprintln 输出内容到字符串
scan:输出函数
scan系列主要用于输入
例在交互式界面中获取用户输入
package main
import "fmt"
func main() {
var name string
fmt.Print("输入你的姓名:")
fmt.Scan(&name)
fmt.Printf("你输入的姓名是:%s", name)
}
//输入你的姓名:王境泽
//你输入的姓名是:王境泽
//值得注意的是,Scan 后面需要使用 &,否则会被视为直接传参进去。
scan.go文件中定义了9个函数:
这9个函数可以扫描格式化文本以生成值。同样也可以按照两个维度来说明。
/*如果把" Scan "理解为核心关键字,那么后面跟的后缀有" f "和" ln "以及"", 着重的是输入内容的结果;如果后缀是" f ", 则指定了format 如果后缀是" ln ", 则有换行符*/
Scanln、Fscanln、Sscanln 读取到换行时停止,并要求一次提供一行所有条目;
Scan、Fscan、Sscan 读取内容时不关注换行;
Scanf、Fscanf、Sscanf 根据格式化文本读取。
/*如果把" Scan "理解为核心关键字,那么前面的前缀有" F "和" S "以及"", 着重的是输入内容的来源(终端);如果前缀是" F ", 则指定了 io.Reader 如果前缀是" S ", 则是从字符串读取*/
Scan、Scanf、Scanln 从标准输入os.Stdin读取文本;
Fscan、Fscanf、Fscanln 从指定的io.Reader接口读取文本;
Sscan、Sscanf、Sscanln 从一个参数字符串读取文本。
边栏推荐
- Integritee通过XCM集成至Moonriver,为其生态系统带来企业级隐私解决方案
- 电脑共享打印机拒绝访问要怎么办
- Talking about cookies of client storage technology
- Dark horse programmer - software testing - 09 stage 2-linux and database -31-43 instructions issued by modifying the file permission letter, - find the link to modify the file, find the file command,
- Practice examples to understand JS strong cache negotiation cache
- Delete the characters with the least number of occurrences in the string [JS, map sorting, regular]
- Form组件常用校验规则-1(持续更新中~)
- 凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
- 软件客户端数字签名一定要申请代码签名证书吗?
- 栈:如何实现有效括号的判断?
猜你喜欢
【深度学习】一文看尽Pytorch之十九种损失函数
太方便了,钉钉上就可完成代码发布审批啦!
MySQL中的日期时间类型与格式化方式
B2B mall system development of electronic components: an example of enabling enterprises to build standardized purchase, sale and inventory processes
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
Flet教程之 08 AppBar工具栏基础入门(教程含源码)
同事的接口文档我每次看着就头大,毛病多多。。。
Process of manually encrypt the mass-producing firmware and programming ESP devices
Neural network IOT platform construction (IOT platform construction practical tutorial)
Cbcgptabwnd control used by BCG (equivalent to MFC TabControl)
随机推荐
漫谈客户端存储技术之Cookie篇
Informatics Olympiad 1336: [example 3-1] find roots and children
Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
栈:如何实现有效括号的判断?
Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
强化学习-学习笔记2 | 价值学习
Employment prospects of neural network Internet of things application technology [welcome to add]
Taishan Office Technology Lecture: about the order of background (shading and highlighting)
输入的查询SQL语句,是如何执行的?
Employment prospects and current situation of Internet of things application technology
Application practice | Shuhai supply chain construction of data center based on Apache Doris
Selected review | machine learning technology for Cataract Classification / classification
Cbcgptabwnd control used by BCG (equivalent to MFC TabControl)
NetCore3.1 Json web token 中间件
电脑共享打印机拒绝访问要怎么办
实践示例理解js强缓存协商缓存
Basic use of kotlin
六石编程学:关于代码,有六个得意