当前位置:网站首页>【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
2022-07-07 22:35:00 【华为云】
一、自动推导类型
1.自动推导类型
自动推导类型,在声明赋值变量时,不需要var和变量类型,其类型是由所赋值来决定的。
package mainimport "fmt"func main() { //初始化 var a int = 10 //赋值 a = 20 //自动推导 b := 30 //输出 fmt.Println(a, b)}
2.总结
- 什么是自动推导类型:在声明赋值变量时,不需要var和变量类型,其类型是由所赋的值来决定的。
- 自动推导类型基本语法格式:“变量名:=值”。
- 多重赋值(使用自动推导类型一次性给多个变量赋值)语法格式:“变量名1,变量名2,变量名3:= 值1,值2,值3"
二、输入和输出
1.输出
1.1 fmt.Print
普通输出
func Print(a ...interface{}) (n int, err error)
- Print采用默认格式将其参数格式化并写入标准输出。
- 如果两个相邻的参数都不是字符串,会在它们的输出之间添加空格。
- 返回写入的字节数和遇到的任何错误。
1.2 fmt.Println
换行输出
func Println(a ...interface{}) (n int, err error)
- Println采用默认格式将其参数格式化并写入标准输出。
- 总是会在相邻参数的输出之间添加空格并在输出结束后添加换行符。
- 返回写入的字节数和遇到的任何错误。
1.3 fmt.Printf
格式化输出
func Printf(format string, a ...interface{}) (n int, err error)
- Printf根据format参数生成格式化的字符串并写入标准输出。
- 返回写入的字节数和遇到的任何错误。
2.输入
2.1 fmt.Scan
普通输入
func Scan(a ...interface{}) (n int, err error)
- Scan从标准输入扫描文本,将成功读取的空白分隔的值保存进成功传递给- 本函数的参数。换行视为空白符。
- 返回成功扫描的条目个数和遇到的任何错误。如果读取的条目比提供的参数少,会返回一个错误报告原因。
案例:
package mainimport "fmt"func main() { var ( name string age int isMale bool ) fmt.Scan(&name, &age, &isMale) fmt.Printf("扫描结果 name:%s age:%d isMarried:%t \n", name, age, isMale)}
2.2 fmt.Scanf
格式化输入
func Scanf(format string, a ...interface{}) (n int, err error)
- Scanf从标准输入扫描文本,根据format 参数指定的格式将成功读取的空白分隔的值保存进成功传递给本函数的参数。
- 返回成功扫描的条目个数和遇到的任何错误。
案例:
package mainimport "fmt"func main(){ var ( name string age int isMale bool ) fmt.Scanf("name:%s age:%d isMale:%v", &name, &age, &isMale) //fmt.Scanf("%s%d%t",&name,&age,&isMale)建议这样使用,输入的时候中间使用空格隔开 fmt.Printf("扫描结果 name:%s age:%d isMarried:%t \n", name, age, isMale)}
2.3 总结
- Scanf()语法格式:fmt.Scanf("%d",&num)
- Scan()语法格式:fmt.Scan(&num)
- 变量地址:在内存中会为变量开辟出对应的存储单元,为了能够找到该存储单元存取 数据,系统会将每个单元加上一个编号,这个编号就是地址
边栏推荐
- Trust orbtk development issues 2022
- 玩轉Sonar
- 大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
- QT adds resource files, adds icons for qaction, establishes signal slot functions, and implements
- Summary of weidongshan phase II course content
- Basic principle and usage of dynamic library, -fpic option context
- 【编程题】【Scratch二级】2019.03 垃圾分类
- 商品的设计等整个生命周期,都可以将其纳入到产业互联网的范畴内
- Zhou Hongqi, 52 ans, est - il encore jeune?
- 什么是负载均衡?DNS如何实现负载均衡?
猜你喜欢
[question de programmation] [scratch niveau 2] oiseaux volants en décembre 2019
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
Coindesk comments on the decentralization process of the wave field: let people see the future of the Internet
paddle一个由三个卷积层组成的网络完成cifar10数据集的图像分类任务
【编程题】【Scratch二级】2019.12 飞翔的小鸟
【编程题】【Scratch二级】2019.03 垃圾分类
Detailed explanation of interview questions: the history of blood and tears in implementing distributed locks with redis
【笔记】常见组合滤波电路
[the most detailed in history] statistical description of overdue days in credit
How to measure whether the product is "just needed, high frequency, pain points"
随机推荐
DNS 系列(一):为什么更新了 DNS 记录不生效?
某马旅游网站开发(对servlet的优化)
华泰证券官方网站开户安全吗?
Robomaster visual tutorial (0) Introduction
2022-07-07:原本数组中都是大于0、小于等于k的数字,是一个单调不减的数组, 其中可能有相等的数字,总体趋势是递增的。 但是其中有些位置的数被替换成了0,我们需要求出所有的把0替换的方案数量:
Two small problems in creating user registration interface
【笔记】常见组合滤波电路
Database query - what is the highest data?
某马旅游网站开发(登录注册退出功能的实现)
new和delete的底层原理以及模板
[programming problem] [scratch Level 2] March 2019 draw a square spiral
Robomaster visual tutorial (1) camera
智慧监管入场,美团等互联网服务平台何去何从
手写一个模拟的ReentrantLock
Introduction knowledge system of Web front-end engineers
Handwriting a simulated reentrantlock
【编程题】【Scratch二级】2019.12 飞翔的小鸟
[C language] objective questions - knowledge points
接口测试进阶接口脚本使用—apipost(预/后执行脚本)
关于组织2021-2022全国青少年电子信息智能创新大赛西南赛区(四川)复赛的通知