当前位置:网站首页>【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
2022-07-07 22:38:00 【InfoQ】
一、自动推导类型
1.自动推导类型
package main
import "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 main
import "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 main
import "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)
- 变量地址:在内存中会为变量开辟出对应的存储单元,为了能够找到该存储单元存取 数据,系统会将每个单元加上一个编号,这个编号就是地址
边栏推荐
- 韦东山第三期课程内容概要
- 【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础
- A brief history of information by James Gleick
- QT establish signal slots between different classes and transfer parameters
- C # generics and performance comparison
- STM32F1與STM32CubeIDE編程實例-旋轉編碼器驅動
- Deep dive kotlin synergy (XXII): flow treatment
- 52岁的周鸿祎,还年轻吗?
- 腾讯安全发布《BOT管理白皮书》|解读BOT攻击,探索防护之道
- [programming problem] [scratch Level 2] December 2019 flying birds
猜你喜欢

去了字节跳动,才知道年薪 40w 的测试工程师有这么多?

【笔记】常见组合滤波电路

An error is reported during the process of setting up ADG. Rman-03009 ora-03113

The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log
![[the most detailed in history] statistical description of overdue days in credit](/img/f7/5c3cbfec5b010171376ac122c704b2.png)
[the most detailed in history] statistical description of overdue days in credit

3 years of experience, can't you get 20K for the interview and test post? Such a hole?

Reptile practice (VIII): reptile expression pack

52岁的周鸿祎,还年轻吗?
![[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

How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
随机推荐
The method of server defense against DDoS, Hangzhou advanced anti DDoS IP section 103.219.39 x
[programming problem] [scratch Level 2] draw ten squares in December 2019
【测试面试题】页面很卡的原因分析及解决方案
[the most detailed in history] statistical description of overdue days in credit
Solution to prompt configure: error: curses library not found when configuring and installing crosstool ng tool
The difference between get and post
Binder核心API
【编程题】【Scratch二级】2019.09 制作蝙蝠冲关游戏
STM32F1与STM32CubeIDE编程实例-旋转编码器驱动
浪潮云溪分布式数据库 Tracing(二)—— 源码解析
Fully automated processing of monthly card shortage data and output of card shortage personnel information
“一个优秀程序员可抵五个普通程序员”,差距就在这7个关键点
Introduction knowledge system of Web front-end engineers
51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up
QT establish signal slots between different classes and transfer parameters
paddle一个由三个卷积层组成的网络完成cifar10数据集的图像分类任务
Robomaster visual tutorial (0) Introduction
paddle入门-使用LeNet在MNIST实现图像分类方法二
C language 001: download, install, create the first C project and execute the first C language program of CodeBlocks
Handwriting a simulated reentrantlock