整数类型
最常用的整数类型是int,它会根据你的系统自动选择32位或64位。
基本用法
Go
func main() {
// int - 最常用的整数类型
var age int = 25
var count = 100 // 类型推断为int
score := 98 // 短声明,也是int
fmt.Printf("年龄: %d, 类型: %T\n", age, age)
fmt.Printf("数量: %d, 类型: %T\n", count, count)
fmt.Printf("分数: %d, 类型: %T\n", score, score)
// 特殊用途的整数类型
var byteValue byte = 'A' // byte 用于表示ASCII字符
var runeValue rune = '中' // rune 用于表示Unicode字符
fmt.Printf("byte: %c (ASCII: %d)\n", byteValue, byteValue)
fmt.Printf("rune: %c (Unicode: %U)\n", runeValue, runeValue)
}
整数运算
Go
func main() {
a, b := 17, 5
// 基本算术运算
fmt.Printf("%d + %d = %d\n", a, b, a+b) // 22
fmt.Printf("%d - %d = %d\n", a, b, a-b) // 12
fmt.Printf("%d * %d = %d\n", a, b, a*b) // 85
fmt.Printf("%d / %d = %d\n", a, b, a/b) // 3(整数除法)
fmt.Printf("%d %% %d = %d\n", a, b, a%b) // 2(取余数)
// 自增自减
counter := 0
counter++ // 自增
fmt.Printf("自增后: %d\n", counter) // 1
counter-- // 自减
fmt.Printf("自减后: %d\n", counter) // 0
}
浮点数类型
处理小数时,我们通常使用float64,它提供了足够的精度满足大多数需求。
基本用法
Go
func main() {
// float64 - 最常用的浮点数类型
var price float64 = 99.99
var temperature = 36.5 // 类型推断为float64
score := 98.5 // 短声明,也是float64
fmt.Printf("价格: %.2f, 类型: %T\n", price, price)
fmt.Printf("温度: %.1f, 类型: %T\n", temperature, temperature)
fmt.Printf("分数: %.1f, 类型: %T\n", score, score)
// 科学计数法
smallNum := 1.23e-4 // 0.000123
bigNum := 5.67e6 // 5670000
fmt.Printf("小数: %f\n", smallNum)
fmt.Printf("大数: %f\n", bigNum)
}
浮点数运算
Go
func main() {
a, b := 10.5, 3.2
// 基本运算
fmt.Printf("%.2f + %.2f = %.2f\n", a, b, a+b) // 13.70
fmt.Printf("%.2f - %.2f = %.2f\n", a, b, a-b) // 7.30
fmt.Printf("%.2f * %.2f = %.2f\n", a, b, a*b) // 33.60
fmt.Printf("%.2f / %.2f = %.2f\n", a, b, a/b) // 3.28
// 注意事项:浮点数精度问题
result := 0.1 + 0.2
fmt.Printf("0.1 + 0.2 = %.20f\n", result) // 0.29999999999999998890
}
布尔类型
布尔类型是Go中最简单的数据类型,只有两个值:true和false。它主要用于条件判断和逻辑运算。
基本用法
Go
package main
import "fmt"
func main() {
// 声明布尔变量
var isActive bool = true
var isEnabled = false // 类型推断
isLogged := true // 短变量声明(最常用)
fmt.Printf("isActive: %v, 类型: %T\n", isActive, isActive)
fmt.Printf("isEnabled: %v, 类型: %T\n", isEnabled, isEnabled)
fmt.Printf("isLogged: %v, 类型: %T\n", isLogged, isLogged)
}
逻辑运算
Go
func main() {
a, b := true, false
// 与运算(&&):全真才真
fmt.Printf("true && false = %v\n", a && b) // false
// 或运算(||):一真即真
fmt.Printf("true || false = %v\n", a || b) // true
// 非运算(!):取反
fmt.Printf("!true = %v\n", !a) // false
// 比较运算产生布尔值
age := 18
isAdult := age >= 18
fmt.Printf("年龄 %d 是否成年: %v\n", age, isAdult) // true
}
字符串类型
字符串是Go中使用最频繁的数据类型之一,它支持丰富的操作。
基本用法
Go
func main() {
// 字符串定义
var name string = "张三"
var city = "北京" // 类型推断
message := "Hello, Go!" // 短声明(最常用)
fmt.Printf("姓名: %s\n", name)
fmt.Printf("城市: %s\n", city)
fmt.Printf("消息: %s\n", message)
// 多行字符串(使用反引号)
multiLine := `
这是第一行
这是第二行
这是第三行
`
fmt.Println("多行字符串:", multiLine)
// 字符串长度
fmt.Printf("'%s' 的长度: %d 字节\n", message, len(message))
}
常用字符串操作
Go
import (
"fmt"
"strings"
)
func main() {
str := "Hello, Go Programming!"
// 1. 字符串连接
s1 := "Hello" + " " + "World"
s2 := fmt.Sprintf("%s %s", "Go", "语言")
fmt.Println("连接1:", s1)
fmt.Println("连接2:", s2)
// 2. 分割字符串
parts := strings.Split(str, ",")
fmt.Printf("分割结果: %v\n", parts)
// 3. 包含判断
contains := strings.Contains(str, "Go")
fmt.Printf("是否包含'Go': %v\n", contains)
// 4. 前缀/后缀
fmt.Printf("以'Hello'开头: %v\n", strings.HasPrefix(str, "Hello"))
fmt.Printf("以'!'结尾: %v\n", strings.HasSuffix(str, "!"))
// 5. 大小写转换
fmt.Printf("大写: %s\n", strings.ToUpper(str))
fmt.Printf("小写: %s\n", strings.ToLower(str))
// 6. 去除空格
dirty := " hello world "
clean := strings.TrimSpace(dirty)
fmt.Printf("原字符串: '%s'\n", dirty)
fmt.Printf("去除空格: '%s'\n", clean)
// 7. 替换
replaced := strings.Replace(str, "Go", "Golang", 1)
fmt.Printf("替换后: %s\n", replaced)
}
字符串格式化
Go
func main() {
name := "李四"
age := 28
score := 95.5
isPass := true
// 格式化输出
fmt.Printf("姓名: %s\n", name)
fmt.Printf("年龄: %d岁\n", age)
fmt.Printf("分数: %.1f分\n", score)
fmt.Printf("是否通过: %t\n", isPass)
// 格式化字符串(不输出)
info := fmt.Sprintf("学生%s,年龄%d,分数%.1f", name, age, score)
fmt.Println(info)
// 各种格式化选项
fmt.Printf("默认格式: %v\n", name)
fmt.Printf("类型: %T\n", name)
fmt.Printf("带引号: %q\n", name)
fmt.Printf("宽度8右对齐: |%8s|\n", name)
fmt.Printf("宽度8左对齐: |%-8s|\n", name)
}
类型转换
Go要求显式类型转换,这让代码更清晰、更安全。
常用类型转换
Go
import (
"fmt"
"strconv"
)
func main() {
// 1. 数值类型转换
var i int = 42
var f float64 = float64(i) // int转float64
var i2 int = int(f) // float64转int(截断小数)
fmt.Printf("int %d -> float64 %.1f\n", i, f)
fmt.Printf("float64 %.1f -> int %d\n", f, i2)
// 2. 整数和字符串转换
num := 123
numStr := strconv.Itoa(num) // int转string
fmt.Printf("int %d -> string %q\n", num, numStr)
str := "456"
strNum, _ := strconv.Atoi(str) // string转int
fmt.Printf("string %q -> int %d\n", str, strNum)
// 3. 浮点数和字符串转换
fNum := 3.14159
fStr := strconv.FormatFloat(fNum, 'f', 2, 64) // float64转string,保留2位小数
fmt.Printf("float64 %.3f -> string %q\n", fNum, fStr)
fStr2 := "2.71828"
fNum2, _ := strconv.ParseFloat(fStr2, 64) // string转float64
fmt.Printf("string %q -> float64 %.5f\n", fStr2, fNum2)
// 4. 布尔和字符串转换
b := true
bStr := strconv.FormatBool(b)
fmt.Printf("bool %v -> string %q\n", b, bStr)
bStr2 := "false"
b2, _ := strconv.ParseBool(bStr2)
fmt.Printf("string %q -> bool %v\n", bStr2, b2)
}
📊 常用数据类型速查表
| 类型 | 关键字 | 默认值 | 常用场景 | 示例 |
|---|---|---|---|---|
| 布尔 | bool |
false |
条件判断、标志位 | isValid := true |
| 整数 | int |
0 |
计数、索引、年龄 | count := 100 |
| 浮点数 | float64 |
0.0 |
分数、价格、测量值 | price := 99.99 |
| 字符串 | string |
"" |
文本、名称、消息 | name := "张三" |
| 字节 | byte |
0 |
ASCII字符 | ch := 'A' |
| 字符 | rune |
0 |
Unicode字符 | ch := '中' |