当前位置:网站首页>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 从一个参数字符串读取文本。
边栏推荐
- Process of manually encrypt the mass-producing firmware and programming ESP devices
- 凌云出海记 | 一零跃动&华为云:共助非洲普惠金融服务
- HMM hidden Markov model and code implementation
- ACM组合计数入门
- 【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
- Free soldier
- ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
- 2022 Health Exhibition, Beijing Health Expo, China Health Exhibition, great health exhibition November 13
- Oracle database, numbers Force 2 decimal places to display-Alibaba Cloud
- QT writing the Internet of things management platform 38- multiple database support
猜你喜欢
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
Ziguang zhanrui completed the first 5g R17 IOT NTN satellite on the Internet of things in the world
实践示例理解js强缓存协商缓存
ACM组合计数入门
Win11怎么搜索无线显示器?Win11查找无线显示器设备的方法
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,
MySQL中的日期时间类型与格式化方式
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
Development and construction of DFI ecological NFT mobile mining system
随机推荐
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
PHP pseudo original API docking method
How to adapt your games to different sizes of mobile screen
Lingyun going to sea | Murong Technology & Huawei cloud: creating a model of financial SaaS solutions in Africa
Creation of JVM family objects
Actual combat simulation │ JWT login authentication
Key rendering paths for performance optimization
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
How is the entered query SQL statement executed?
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Pointnext: review pointnet through improved model training and scaling strategies++
Why is the maximum speed the speed of light
C server log module
Data set division
Employment prospects of neural network Internet of things application technology [welcome to add]
哈希(Hash)竞猜游戏系统开发功能分析及源码
凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
The problem of the maximum difference between the left and right maxima
Oracle database, numbers Force 2 decimal places to display-Alibaba Cloud
Qt编写物联网管理平台38-多种数据库支持