整型
整型分为以下两个大类:
按长度分为:int8、int16、int32、int64
对应的无符号整型:uint8、uint16、uint32、uint64
浮点型
Go语言支持两种浮点型数:float32和float64
复数
complex64和complex128
布尔值
布尔型数据只有true(真)和false(假)两个值
注意:
1、布尔类型变量的默认值为false。
2、Go 语言中不允许将整型强制转换为布尔型.
3、布尔型无法参与数值运算,也无法与其他类型进行转换。
字符串
go
s1 := "hello"
s2 := "你好"
armasm
// 字符串连接
combined := s1 + " " + s2 // 结果为 "hello 你好"
// 获取字符串长度
length1 := len(s1) // 英文长度为 5
length2 := len(s2) // 中文长度为 2(UTF-8 编码下每个中文字符占 3 字节,但 len 返回字符数)
// 字符串比较
isEqual := (s1 == s2) // 结果为 false
字符串拼接
使用加号(+)或字符串的 join() 方法可以将多个字符串连接起来。加号适用于少量字符串拼接,而 join() 方法在处理大量字符串时效率更高。
python
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2 # 输出: "Hello World"
words = ["Hello", "World"]
result = " ".join(words) # 输出: "Hello World"
字符串分割
split() 方法可以将字符串按指定分隔符拆分为列表。若不指定分隔符,默认按空白字符分割。
python
text = "Hello,World"
parts = text.split(",") # 输出: ["Hello", "World"]
字符串替换
replace() 方法用于替换字符串中的子串。可以指定替换次数。
python
text = "Hello World"
new_text = text.replace("World", "Python") # 输出: "Hello Python"
大小写转换
upper() 和 lower() 方法分别将字符串转换为全大写或全小写。capitalize() 将首字母大写,其余小写。
python
text = "Hello World"
upper_text = text.upper() # 输出: "HELLO WORLD"
lower_text = text.lower() # 输出: "hello world"
去除空白字符
strip() 方法去除字符串两端的空白字符(包括空格、换行符等)。lstrip() 和 rstrip() 分别去除左侧或右侧的空白。
python
text = " Hello World "
stripped = text.strip() # 输出: "Hello World"
字符串查找
find() 方法返回子串首次出现的索引,未找到时返回 -1。index() 方法类似,但未找到时会抛出异常。
python
text = "Hello World"
position = text.find("World") # 输出: 6
字符串格式化
使用 format() 方法或 f-string(Python 3.6+)可以动态插入变量值。
python
name = "Alice"
age = 25
# format() 方法
message = "My name is {} and I am {} years old".format(name, age)
# f-string
message = f"My name is {name} and I am {age} years old"
字符串长度
len() 函数返回字符串的字符数。
python
text = "Hello"
length = len(text) # 输出: 5
字符串切片
通过索引和切片操作可以获取子串。索引从 0 开始,支持负数索引(从末尾开始计数)。
python
text = "Hello World"
substring = text[0:5] # 输出: "Hello"
last_char = text[-1] # 输出: "d"
检查字符串内容
startswith() 和 endswith() 检查字符串是否以指定子串开头或结尾。
isalpha()、isdigit() 等方法检查字符串是否全为字母或数字。
python
text = "Hello"
is_alpha = text.isalpha() # 输出: True
starts_with_he = text.startswith("He") # 输出: True
byte和rune类型
byte用于单字节数据,rune用于 Unicode 字符。- 在处理字符串时,优先使用
rune以避免多字节字符问题。 - 类型转换时需注意数据范围
go
s := "Hello, 世界"
for i := 0; i < len(s); i++ {
fmt.Printf("%c ", s[i]) // 用 byte 遍历会乱码
}
fmt.Println()
for _, r := range s {
fmt.Printf("%c ", r) // 用 rune 遍历正常
}
字符串修改
ASCII字符串修改(使用[]byte)
go
s1 := "big"
byteS1 := []byte(s1) // 转换为字节切片
byteS1[0] = 'p' // 修改第一个字节
fmt.Println(string(byteS1)) // 输出: pig
Unicode字符串修改(使用[]rune)
go
s2 := "白萝卜"
runeS2 := []rune(s2) // 转换为符文切片
runeS2[0] = '红' // 修改第一个符文
fmt.Println(string(runeS2)) // 输出: 红萝卜
关键区别
[]byte按字节处理,适合ASCII字符(每个字符1字节)[]rune按Unicode码点处理,适合多字节字符(如中文,每个字符3字节)- 错误使用
[]byte处理中文会导致乱码(因为会拆散多字节字符)
性能考虑
[]byte转换更高效,但仅适用于单字节字符集[]rune转换会进行完整的Unicode解码,开销较大- 修改后都需要通过
string()转换回字符串类型
类型转换
go
var a, b = 3, 4
var c int
c = int(math.Sqrt(float64(a*a + b*b)))
a*a + b*b计算两直角边平方和(3² + 4² = 25)float64()将整型结果转换为float64类型(math.Sqrt要求的参数类型)math.Sqrt()计算平方根(√25 = 5.0)int()将浮点结果转换回整型
go
fmt.Println(c)
//打印计算结果5(整型)。