6、键盘输入和打印输出
1.打印输出
Print()
单行输出
Println()
换行输出
Printf()
格式化输出占位符:
- %v,原样输出
- %T,打印类型
- %t,bool类型
- %s,字符串
- %f,浮点
- %d,10进制的整数
- %b,2进制的整数
- %x,%X,16进制
- %x:0-9,a-f
- %X:0-9,A-F
- %c,打印字符
- %P,打印地址
......
a := 10
b := "str"
c := true
d := 3.14
e := `Java`
f := 'A'
fmt.Printf("a类型为:%T \t a的值为:%d \t a的地址为:%p \r\n", a, a, a)
fmt.Printf("b类型为:%T \t b的值为:%s \t b的地址为:%p \r\n", b, b, b)
fmt.Printf("c类型为:%T \t c的值为:%t \t c的地址为:%p \r\n", c, c, c)
fmt.Printf("d类型为:%T \t d的值为:%f \t d的地址为:%p \r\n", d, d, d)
fmt.Printf("e类型为:%T \t e的值为:%s \t e的地址为:%p \r\n", e, e, e)
fmt.Printf("f类型为:%T \t f的值为:%c \t f的地址为:%p \r\n", f, f, f)
fmt.Println("原样输出")
fmt.Printf("a:%v \r\n", a)
fmt.Printf("b:%v \r\n", b)
fmt.Printf("c:%v \r\n", c)
fmt.Printf("d:%v \r\n", d)
fmt.Printf("e:%v \r\n", e)
fmt.Printf("f:%v \r\n", f)

2.键盘输出
Scanln()
var x int
var y float64
fmt.Println("请输入一个整数:,一个浮点数:")
fmt.Scanln(&x, &y) // 读取键盘的输入,通过操作地址,赋值给x和y,阻塞式的
fmt.Printf("x = %d ,y = %f \n", x, y)

Scanf()
fmt.Scanf("%d,%f", &x, &y) // 按照指定固定格式键盘输入
fmt.Printf("x = %d,y = %f \n", x, y)

bufio
fmt.Println("请输入一个字符串:")
reader := bufio.NewReader(os.Stdin)
s1, _ := reader.ReadString('\n')
fmt.Println("读取到的数据:", s1)

浙公网安备 33010602011771号