当前位置:网站首页>Problems encountered by scan, scanf and scanln in golang
Problems encountered by scan, scanf and scanln in golang
2022-07-04 09:16:00 【The diligent bird that flies first】
Problem description :
func main(){
var input byte
fmt.Println(" Please enter a letter ")
scan, err := fmt.Scan(&input)
if err != nil {
fmt.Println("err:", err)
}
fmt.Println(scan)
fmt.Printf("the anser is %c", input)
}
This defines a variable to accept byte But I can't accept , The errors that occur are as follows
Please enter a letter
a
err: expected integer
0
the anser is
Expect to accept integers , This is because byte The essence of type is uint8 Type of , So you should pass in an integer , But you still need to pass in the character type have access to scanf Conduct , The code is as follows .
func main(){
var input byte
fmt.Println(" Please enter a letter ")
_, err := fmt.Scanf("%c", &input)
if err != nil {
fmt.Println("err:", err)
}
fmt.Printf("the anser is %c", input)
}
The specific output is
Please enter a letter
a
the anser is a
Process finished with exit code 0
problem 2:
func main(){
var input byte
fmt.Println(" Please enter a letter ")
fmt.Scanf("input: %c", &input)
fmt.Println(input)
switch input {
case 'a':
fmt.Println(" Today is Monday ")
fmt.Println(" It's time to go to school ")
case 'b':
fmt.Println(" Today is Tuesday ")
case 'c':
fmt.Println(" Today is Wednesday ")
default:
fmt.Println(" Today is the weekend ")
}
}
The terminal should also follow scanf Write the style of the first parameter in , Just replace the placeholder with something you need .
Please enter a letter
input: a
It needs to be written in this way to be recognized as a
边栏推荐
- 《网络是怎么样连接的》读书笔记 - Tcp/IP连接(二)
- Target detection -- intensive reading of yolov3 paper
- Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
- Trim leading or trailing characters from strings- Trim leading or trailing characters from a string?
- Awk from entry to earth (15) awk executes external commands
- What is inner connection and outer connection? What are the uses and benefits
- 165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
- Sword finger offer 30 contains the stack of Min function
- Awk from getting started to digging in (11) detailed explanation of awk getline function
- C语言-入门-基础-语法-[主函数,头文件](二)
猜你喜欢
How do microservices aggregate API documents? This wave of show~
Mac platform forgets the root password of MySQL
If you can quickly generate a dictionary from two lists
AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human
What exactly is DAAS data as a service? Don't be misled by other DAAS concepts
How should PMP learning ideas be realized?
GoLand environment variable configuration
Live in a dream, only do things you don't say
Sequence model
awk从入门到入土(12)awk也可以写脚本,替代shell
随机推荐
Awk from digging into the ground to getting started (10) awk built-in functions
Tkinter Huarong Road 4x4 tutorial II
2022-2028 global probiotics industry research and trend analysis report
awk从入门到入土(15)awk执行外部命令
AMLOGIC gsensor debugging
Awk from getting started to digging in (11) detailed explanation of awk getline function
Some points needing attention in PMP learning
Implementation principle of redis string and sorted set
Reading notes on how to connect the network - hubs, routers and routers (III)
Awk from entry to earth (8) array
2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
《网络是怎么样连接的》读书笔记 - FTTH
What is permission? What is a role? What are users?
地平线 旭日X3 PI (一)首次开机细节
awk从入门到入土(11)awk getline函数详解
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
Service call feign of "micro service"
awk从入门到入土(7)条件语句
Review of last week's hot spots (6.27-7.3)
随机事件的关系与运算