当前位置:网站首页>【愚公系列】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)
- 变量地址:在内存中会为变量开辟出对应的存储单元,为了能够找到该存储单元存取 数据,系统会将每个单元加上一个编号,这个编号就是地址
边栏推荐
- Anaconda+pycharm+pyqt5 configuration problem: pyuic5 cannot be found exe
- 3 years of experience, can't you get 20K for the interview and test post? Such a hole?
- How to add automatic sorting titles in typora software?
- Qt添加资源文件,为QAction添加图标,建立信号槽函数并实现
- When creating body middleware, express Is there any difference between setting extended to true and false in urlencoded?
- 应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
- 35岁真就成了职业危机?不,我的技术在积累,我还越吃越香了
- fabulous! How does idea open multiple projects in a single window?
- 【obs】Impossible to find entrance point CreateDirect3D11DeviceFromDXGIDevice
- 动态库基本原理和使用方法,-fPIC 选项的来龙去脉
猜你喜欢

Relevant methods of sorting arrays in JS (if you want to understand arrays, it's enough to read this article)

Development of a horse tourism website (optimization of servlet)

52岁的周鸿祎,还年轻吗?

ReentrantLock 公平锁源码 第0篇

Anaconda+pycharm+pyqt5 configuration problem: pyuic5 cannot be found exe

玩转Sonar
![[basis of recommendation system] sampling and construction of positive and negative samples](/img/4b/753a61b583cf38826b597fd31e5d20.png)
[basis of recommendation system] sampling and construction of positive and negative samples

STM32F1與STM32CubeIDE編程實例-旋轉編碼器驅動

"An excellent programmer is worth five ordinary programmers", and the gap lies in these seven key points

An error is reported during the process of setting up ADG. Rman-03009 ora-03113
随机推荐
Is Zhou Hongyi, 52, still young?
[C language] objective questions - knowledge points
Visual Studio Deployment Project - Create shortcut to deployed executable
QT establish signal slots between different classes and transfer parameters
How to measure whether the product is "just needed, high frequency, pain points"
Stm32f1 and stm32cubeide programming example - rotary encoder drive
某马旅游网站开发(登录注册退出功能的实现)
Notice on organizing the second round of the Southwest Division (Sichuan) of the 2021-2022 National Youth electronic information intelligent innovation competition
Use filters to count URL request time
Basic principle and usage of dynamic library, -fpic option context
1293_FreeRTOS中xTaskResumeAll()接口的实现分析
[the most detailed in history] statistical description of overdue days in credit
QT adds resource files, adds icons for qaction, establishes signal slot functions, and implements
Relevant methods of sorting arrays in JS (if you want to understand arrays, it's enough to read this article)
Is 35 really a career crisis? No, my skills are accumulating, and the more I eat, the better
51与蓝牙模块通讯,51驱动蓝牙APP点灯
STM32F1與STM32CubeIDE編程實例-旋轉編碼器驅動
攻防世界Web进阶区unserialize3题解
Tencent security released the white paper on BOT Management | interpreting BOT attacks and exploring ways to protect
备库一直有延迟,查看mrp为wait_for_log,重启mrp后为apply_log但过一会又wait_for_log