当前位置:网站首页>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
边栏推荐
- At the age of 30, I changed to Hongmeng with a high salary because I did these three things
- C language - Introduction - Foundation - syntax - [main function, header file] (II)
- awk从入土到入门(10)awk内置函数
- Global and Chinese market of bipolar generators 2022-2028: Research Report on technology, participants, trends, market size and share
- Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
- LeetCode 74. Search 2D matrix
- Reading notes of how the network is connected - understanding the basic concepts of the network (I)
- awk从入门到入土(7)条件语句
- Jianzhi offer 09 realizes queue with two stacks
- Sword finger offer 30 contains the stack of Min function
猜你喜欢

Implementation principle of redis string and sorted set
![C language - Introduction - Foundation - syntax - [operators, type conversion] (6)](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)

2022-2028 global tensile strain sensor industry research and trend analysis report

Educational Codeforces Round 115 (Rated for Div. 2)

Industry depression has the advantages of industry depression

HMS core helps baby bus show high-quality children's digital content to global developers

After unplugging the network cable, does the original TCP connection still exist?

Explain TCP protocol in detail three handshakes and four waves

Mantis creates users without password options

2022-2028 global strain gauge pressure sensor industry research and trend analysis report
随机推荐
Awk from getting started to digging in (9) circular statement
随机事件的关系与运算
awk从入门到入土(18)gawk线上手册
How do microservices aggregate API documents? This wave of show~
Awk from getting started to digging in (4) user defined variables
《网络是怎么样连接的》读书笔记 - 认识网络基础概念(一)
2022-2028 global visual quality analyzer industry research and trend analysis report
awk从入门到入土(15)awk执行外部命令
Use Alibaba cloud NPM image acceleration
China battery grade manganese sulfate Market Forecast and strategic consulting report (2022 Edition)
Tkinter Huarong Road 4x4 tutorial II
Solve the problem of "Chinese garbled MySQL fields"
HMS core helps baby bus show high-quality children's digital content to global developers
Awk from entry to earth (15) awk executes external commands
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
Implementation principle of redis string and sorted set
Global and Chinese market of planar waveguide optical splitter 2022-2028: Research Report on technology, participants, trends, market size and share
什么是uid?什么是Auth?什么是验证器?
The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
PMP registration process and precautions