网站建设开发感想,学校校园网站 资源建设方案,wordpress客户端5.5,福建建设银行官方网站编程笔记 Golang基础 013 格式化输入输出 一、格式化输出1. fmt.Print系列函数2. Printf格式说明3. 格式化布尔类型 二、格式化输入1. fmt.Scan系列函数注意事项 三、练习小结 Go语言中的格式化输入和输出主要通过标准库 fmt 包来实现。主要是输出需要格式化。 一、格式化输出 … 编程笔记 Golang基础 013 格式化输入输出 一、格式化输出1. fmt.Print系列函数2. Printf格式说明3. 格式化布尔类型 二、格式化输入1. fmt.Scan系列函数注意事项 三、练习小结 Go语言中的格式化输入和输出主要通过标准库 fmt 包来实现。主要是输出需要格式化。 一、格式化输出
1. fmt.Print系列函数
fmt.Println: 输出内容并自动添加换行符。fmt.Printf: 格式化的输出允许按照指定的格式字符串打印变量值。fmt.Print: 输出内容不添加换行符。
package main
import fmtfunc main() {fmt.Println(Hello, World!) // 自动添加换行fmt.Printf(Today is %s\n, 2024年2月21日) // 格式化输出日期fmt.Print(No newline here) // 不会自动添加换行
}2. Printf格式说明
Printf 的格式字符串中可以包含特殊的占位符如 %d, %s, %v, %T 等它们会被相应类型的变量替换
%d: 整数%f 或 %g: 浮点数或科学计数法表示的数字%t: 布尔类型%s: 字符串%v: 值的默认格式表示根据不同类型显示%#v: 类似 %v但输出结构体时会包含字段名%T: 输出值的类型名称%q: 双引号包裹的字符串字面量形式适用于字符串和字符%p: 指针的内存地址
示例
fmt.Printf(%d, %.2f, %t, %s, %#v, %T\n, 123, 3.14159, true, Golang, struct{A int}{A: 42}, myVar)3. 格式化布尔类型
布尔类型的值在使用 %t 格式化时会输出 true 或 false。
var isDone bool false
fmt.Printf(Task is done? %t\n, isDone) // 输出Task is done? false二、格式化输入
1. fmt.Scan系列函数
fmt.Scan: 从标准输入读取数据到一系列变量中空格作为分隔符。fmt.Scanf: 类似于 C 中的 scanf接受一个格式字符串和对应变量列表根据格式进行输入。fmt.Scanln: 类似于 Scanf但在读取一行后停止并丢弃结尾的换行符。fmt.Sscan: 从字符串中扫描。fmt.Sscanf: 类似于 Scanf但是针对字符串而不是标准输入。fmt.Sscanln: 对字符串进行类似 Scanln 的操作。
示例
var name string
var age int
fmt.Println(请输入姓名和年龄用空格分隔)
_, _ fmt.Scan(name, age) // 从键盘输入读取姓名和年龄// 或者使用 Sscanf 从已知字符串读取
input : Alice 30
fmt.Sscanf(input, %s %d, name, age)注意事项
使用格式化输入时务必注意安全性和有效性检查确保输入的数据能够正确转换为预期类型。在处理用户输入时应尽可能避免直接将输入用于数据库查询等敏感操作防止 SQL 注入等问题。Go 语言推荐使用 fmt.Scan 和 fmt.Scanln 的变体 fmt.Scanf 和 fmt.Scanf 以及其配套的格式字符串进行精确控制输入解析。
三、练习
type user struct {name string
}func main() {u : user{guo}//Printf 格式化输出fmt.Printf(% v\n, u) //格式化输出结构fmt.Printf(%#v\n, u) //输出值的 Go 语言表示方法fmt.Printf(%T\n, u) //输出值的类型的 Go 语言表示fmt.Printf(%t\n, true) //输出值的 true 或 falsefmt.Printf(%b\n, 1024) //二进制表示fmt.Printf(%c\n, 11111111) //数值对应的 Unicode 编码字符fmt.Printf(%d\n, 10) //十进制表示fmt.Printf(%o\n, 8) //八进制表示fmt.Printf(%q\n, 22) //转化为十六进制并附上单引号fmt.Printf(%x\n, 1223) //十六进制表示用a-f表示fmt.Printf(%X\n, 1223) //十六进制表示用A-F表示fmt.Printf(%U\n, 1233) //Unicode表示fmt.Printf(%b\n, 12.34) //无小数部分两位指数的科学计数法6946802425218990p-49fmt.Printf(%e\n, 12.345) //科学计数法e表示fmt.Printf(%E\n, 12.34455) //科学计数法E表示fmt.Printf(%f\n, 12.3456) //有小数部分无指数部分fmt.Printf(%g\n, 12.3456) //根据实际情况采用%e或%f输出fmt.Printf(%G\n, 12.3456) //根据实际情况采用%E或%f输出fmt.Printf(%s\n, wqdew) //直接输出字符串或者[]bytefmt.Printf(%q\n, dedede) //双引号括起来的字符串fmt.Printf(%x\n, abczxc) //每个字节用两字节十六进制表示a-f表示fmt.Printf(%X\n, asdzxc) //每个字节用两字节十六进制表示A-F表示fmt.Printf(%p\n, 0x123) //0x开头的十六进制数表示
}小结
初学必备知识。实际开发中也会经常用到。 文章转载自: http://www.morning.fwnyz.cn.gov.cn.fwnyz.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.yqzyp.cn.gov.cn.yqzyp.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.rnqbn.cn.gov.cn.rnqbn.cn http://www.morning.zjcmr.cn.gov.cn.zjcmr.cn http://www.morning.pdmml.cn.gov.cn.pdmml.cn http://www.morning.cfccp.cn.gov.cn.cfccp.cn http://www.morning.yqfdl.cn.gov.cn.yqfdl.cn http://www.morning.lkjzz.cn.gov.cn.lkjzz.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.nqwz.cn.gov.cn.nqwz.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.ftlgy.cn.gov.cn.ftlgy.cn http://www.morning.bbtn.cn.gov.cn.bbtn.cn http://www.morning.ktpzb.cn.gov.cn.ktpzb.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.ptdzm.cn.gov.cn.ptdzm.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.yfzld.cn.gov.cn.yfzld.cn http://www.morning.wmfmj.cn.gov.cn.wmfmj.cn http://www.morning.ktmpw.cn.gov.cn.ktmpw.cn http://www.morning.zlhbg.cn.gov.cn.zlhbg.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.bpds.cn.gov.cn.bpds.cn http://www.morning.grqlc.cn.gov.cn.grqlc.cn http://www.morning.c7491.cn.gov.cn.c7491.cn http://www.morning.knscf.cn.gov.cn.knscf.cn http://www.morning.fllx.cn.gov.cn.fllx.cn http://www.morning.qsy36.cn.gov.cn.qsy36.cn http://www.morning.qfdyt.cn.gov.cn.qfdyt.cn http://www.morning.cznsq.cn.gov.cn.cznsq.cn http://www.morning.zympx.cn.gov.cn.zympx.cn http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.csxlm.cn.gov.cn.csxlm.cn http://www.morning.ysgnb.cn.gov.cn.ysgnb.cn http://www.morning.kxscs.cn.gov.cn.kxscs.cn http://www.morning.ghryk.cn.gov.cn.ghryk.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.qhrdx.cn.gov.cn.qhrdx.cn http://www.morning.bkqdg.cn.gov.cn.bkqdg.cn http://www.morning.dmhs.cn.gov.cn.dmhs.cn http://www.morning.zyffq.cn.gov.cn.zyffq.cn http://www.morning.dmsxd.cn.gov.cn.dmsxd.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.lzdbb.cn.gov.cn.lzdbb.cn http://www.morning.qczjc.cn.gov.cn.qczjc.cn http://www.morning.xrct.cn.gov.cn.xrct.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.yrdt.cn.gov.cn.yrdt.cn http://www.morning.mbbgk.com.gov.cn.mbbgk.com http://www.morning.snnb.cn.gov.cn.snnb.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn http://www.morning.mtrfz.cn.gov.cn.mtrfz.cn http://www.morning.nclps.cn.gov.cn.nclps.cn http://www.morning.dbrnl.cn.gov.cn.dbrnl.cn http://www.morning.jxltk.cn.gov.cn.jxltk.cn http://www.morning.hfrbt.cn.gov.cn.hfrbt.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.pbmg.cn.gov.cn.pbmg.cn http://www.morning.fwllb.cn.gov.cn.fwllb.cn http://www.morning.saastob.com.gov.cn.saastob.com http://www.morning.monstercide.com.gov.cn.monstercide.com http://www.morning.rdkqt.cn.gov.cn.rdkqt.cn http://www.morning.mdwlg.cn.gov.cn.mdwlg.cn http://www.morning.tgyzk.cn.gov.cn.tgyzk.cn http://www.morning.dxrbp.cn.gov.cn.dxrbp.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.dmldp.cn.gov.cn.dmldp.cn http://www.morning.wdply.cn.gov.cn.wdply.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn