当前位置:网站首页>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
边栏推荐
- Global and Chinese markets of hemoglobin analyzers in care points 2022-2028: Research Report on technology, participants, trends, market size and share
- After unplugging the network cable, does the original TCP connection still exist?
- Function comparison between cs5261 and ag9310 demoboard test board | cost advantage of cs5261 replacing ange ag9310
- How to batch change file extensions in win10
- Service call feign of "micro service"
- GoLand environment variable configuration
- Flutter tips: various fancy nesting of listview and pageview
- Awk from entry to penetration (6) regular matching
- Analysis report on the production and marketing demand and investment forecast of tellurium dioxide in the world and China Ⓣ 2022 ~ 2027
- Implementation principle of redis string and sorted set
猜你喜欢
Target detection -- intensive reading of yolov3 paper
[untitled] forwarding least square method
2022-2028 global strain gauge pressure sensor industry research and trend analysis report
AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human
到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了
C语言-入门-基础-语法-[变量,常亮,作用域](五)
After unplugging the network cable, does the original TCP connection still exist?
You can see the employment prospects of PMP project management
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
Mantis creates users without password options
随机推荐
GoLand environment variable configuration
The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
20220701 barbarat lemma proof
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
awk从入门到入土(9)循环语句
UML sequence diagram [easy to understand]
26. Delete duplicates in the ordered array (fast and slow pointer de duplication)
Dede plug-in (multi-function integration)
Codeforces Round #803 (Div. 2)(A-D)
Awk from entry to earth (18) GAW K line manual
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
C语言-入门-基础-语法-[变量,常亮,作用域](五)
2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors
Research Report on the current market situation and development prospects of calcium sulfate whiskers in China (2022 Edition)
China battery grade manganese sulfate Market Forecast and strategic consulting report (2022 Edition)
Awk from entry to soil (5) simple condition matching
Mantis creates users without password options
什么是uid?什么是Auth?什么是验证器?
awk从入门到入土(8)数组
2022-2028 global small batch batch batch furnace industry research and trend analysis report