Golang基础类型占位符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main

import "fmt"

/*认识基本数据类型*/
func main041() {

//整型
var a byte = 123
var b rune = 123
var c int = 123
var d uint = 123
fmt.Println(a,b,c,d)

//浮点型
var rmb float32 = 1.23
//var atomRadius float64 = 1.234567890
var atomRadius = 1.234567890
fmt.Println(rmb,atomRadius)

//字符串型
var name string = "张三"
fmt.Println(name)

//布尔型
var isClever = true
var isBrave bool = false
fmt.Println(isClever,isBrave)

/*查看变量的类型*/
fmt.Printf("a的类型是%T\n",a)
fmt.Printf("isClever的类型是%T\n",isClever)
fmt.Printf("atomRadius的类型是%T\n",atomRadius)
fmt.Printf("1.23的类型是%T\n",1.23)
fmt.Printf("你妹的类型是%T","你妹")
fmt.Printf("3+4i的类型是%T",3+4i)
}

/*
基本类型占位符
%s 字符串占位符
%d 整型占位符(十进制)
%f 浮点型占位符,%.2f精确到小数点后两位
%t 布尔占位符
*/
func main() {
//使用占位符进行输出
fmt.Printf("我是%s,今年%d岁了,比特币余额%.2f,我美吗?——%t\n","比尔",18,1.234567890,false)

//输出时使用5字符宽度(默认右对齐)
fmt.Printf("我是%5s,今年%5d岁了,比特币余额%5.2f,我美吗?——%5t\n","比尔",18,1.234567890,false)

//5位字符宽度,左对齐
fmt.Printf("我是%-5s,今年%-5d岁了,比特币余额%-5.2f,我美吗?——%-5t\n","比尔",18,1.234567890,false)

//打印并换行
fmt.Println("陈述完毕!")
}