Go three input functions

The first type: fmt.Scan (address list)
parameter is passed into the address list. Spaces or line
breaks can be used between input variables. The second type: fmt.Scanln (address list)
is different from Scan in that it comes with line breaks , so line breaks cannot be used between input variables, otherwise the input will be completed.
The third type: fmt.Scanf ("format string", address list)
restricts a fixed input format. You can refer to the running results. The
sample source code is as follows:

package main

import (
	"fmt"
	"strconv"
)

//从终端获取用户的输入内容

func main() {
    
    
var (
	name    string
	age     int
)
fmt.Scan(&name, &age)
//fmt.Scanln(&name,&age)
//fmt.Scanf("name:%s age:%d\n",&name,&age)  //严格按照输出格式进行输出
	d := strconv.Itoa(age)  //数字转换为字符串
	fmt.Println("我的名字叫"+name,"今年"+d+"岁啦")
	
}

Scan demo results:
Insert picture description here
Scanln results:
Insert picture description here

Scanf results:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45701131/article/details/107804285