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)

image

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)

image

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

image

bufio
fmt.Println("请输入一个字符串:")
reader := bufio.NewReader(os.Stdin)
s1, _ := reader.ReadString('\n')
fmt.Println("读取到的数据:", s1)
posted @   我也有梦想呀  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2022-03-26 SpringCloudAlibaba-OSS文件上传
点击右上角即可分享
微信分享提示