全部学科
NodeJS全栈
nodejs
Python全栈
python
小程序首页
📅 2026-05-14 5 分钟 ✍️ juanwangdev

Go strconv包

Go标准库strconv实现字符串与基本类型的相互转换。

Parse系列:字符串转数值

strconv.Atoi

Go
import "strconv"

// 字符串转整数
n, err := strconv.Atoi("42")
if err != nil {
    log.Fatal(err)
}
fmt.Println(n)  // 42

// 失败示例
n, err = strconv.Atoi("abc")  // err != nil

strconv.ParseInt

Go
// 解析整数(指定进制和位数)
n, _ := strconv.ParseInt("42", 10, 64)    // 十进制,64位
fmt.Println(n)  // 42

n, _ = strconv.ParseInt("1010", 2, 64)    // 二进制
fmt.Println(n)  // 10

n, _ = strconv.ParseInt("ff", 16, 64)     // 十六进制
fmt.Println(n)  // 255

// 参数:字符串,进制(0/2/8/10/16),位数

strconv.ParseFloat

Go
// 解析浮点数
f, _ := strconv.ParseFloat("3.14", 64)
fmt.Println(f)  // 3.14

f, _ = strconv.ParseFloat("1.23e10", 64)  // 科学计数法
fmt.Println(f)  // 1.23e+10

// 参数:字符串,位数(32/64)

strconv.ParseBool

Go
// 解析布尔值
b, _ := strconv.ParseBool("true")   // true
b, _ = strconv.ParseBool("1")       // true
b, _ = strconv.ParseBool("false")   // false
b, _ = strconv.ParseBool("0")       // false

// 支持:1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False

Format系列:数值转字符串

strconv.Itoa

Go
// 整数转字符串
s := strconv.Itoa(42)
fmt.Println(s)  // "42"

// 简化的ParseInt

strconv.FormatInt

Go
// 整数转字符串(指定进制)
s := strconv.FormatInt(42, 10)   // "42"
s = strconv.FormatInt(10, 2)     // "1010"
s = strconv.FormatInt(255, 16)   // "ff"

// 参数:整数,进制

strconv.FormatFloat

Go
// 浮点数转字符串
s := strconv.FormatFloat(3.14, 'f', 2, 64)
fmt.Println(s)  // "3.14"

// 参数:浮点数,格式(f/e/E/g/G),精度,位数
// 'f':普通格式
// 'e':科学计数法小写e
// 'E':科学计数法大写E
// 'g':紧凑格式

strconv.FormatBool

Go
// 布尔值转字符串
s := strconv.FormatBool(true)   // "true"
s = strconv.FormatBool(false)   // "false"

Parse函数表

函数用途参数
Atoi字符串→int字符串
ParseInt字符串→int64字符串, 进制, 位数
ParseFloat字符串→float64字符串, 位数
ParseBool字符串→bool字符串

Format函数表

函数用途参数
Itoaint→字符串整数
FormatIntint64→字符串整数, 进制
FormatFloatfloat64→字符串浮点数, 格式, 精度, 位数
FormatBoolbool→字符串布尔值

Append系列:追加到字节切片

strconv.AppendInt

Go
// 整数追加到字节切片
buf := []byte("value: ")
buf = strconv.AppendInt(buf, 42, 10)
fmt.Println(buf)  // "value: 42"

strconv.AppendFloat

Go
// 浮点数追加
buf := []byte("price: ")
buf = strconv.AppendFloat(buf, 3.14, 'f', 2, 64)
fmt.Println(buf)  // "price: 3.14"

strconv.AppendBool

Go
// 布尔值追加
buf := []byte("status: ")
buf = strconv.AppendBool(buf, true)
fmt.Println(buf)  // "status: true"

Quote系列:字符串引号处理

strconv Quote

Go
// 字符串加引号
s := strconv.Quote("hello")
fmt.Println(s)  // "\"hello\""

// 处理特殊字符
s = strconv.Quote("hello\nworld")
fmt.Println(s)  // "\"hello\\nworld\""

strconv.QuoteToASCII

Go
// 引号并转义非ASCII字符
s := strconv.QuoteToASCII("hello世界")
fmt.Println(s)  // "\"hello\\u4e16\\u754c\""

strconv.Unquote

Go
// 移除引号
s, _ := strconv.Unquote("\"hello\"")
fmt.Println(s)  // "hello"

// 处理转义字符
s, _ = strconv.Unquote("\"hello\\nworld\"")
fmt.Println(s)  // "hello\nworld"

常用转换示例

Go
// 字符串转整数
n, _ := strconv.Atoi("100")

// 整数转字符串
s := strconv.Itoa(100)

// 字符串转浮点
f, _ := strconv.ParseFloat("3.14", 64)

// 浮点转字符串
s := strconv.FormatFloat(3.14, 'f', 2, 64)

// 字符串转布尔
b, _ := strconv.ParseBool("true")

// 布尔转字符串
s := strconv.FormatBool(b)

错误处理

NumError类型

Go
// Parse函数返回NumError
n, err := strconv.Atoi("abc")
if err != nil {
    if numErr, ok := err.(*strconv.NumError); ok {
        fmt.Println(numErr.Func)   // "Atoi"
        fmt.Println(numErr.Num)    // "abc"
        fmt.Println(numErr.Err)    // 错误详情
    }
}

快速转换对比

操作函数简化版
字符串→intParseIntAtoi
int→字符串FormatIntItoa
字符串→floatParseFloat
float→字符串FormatFloat

基数转换

不同进制转换

Go
// 二进制字符串转十进制
n, _ := strconv.ParseInt("1010", 2, 64)  // 10

// 十进制转十六进制字符串
s := strconv.FormatInt(255, 16)          // "ff"

// 十六进制字符串转十进制
n, _ := strconv.ParseInt("ff", 16, 64)   // 255

// 进制参数:2, 8, 10, 16
// 进制0表示自动识别前缀(0x十六进制, 0八进制)

要点总结

  • Atoi字符串转整数,Itoa整数转字符串
  • ParseInt支持多种进制解析
  • ParseFloat解析浮点数
  • ParseBool支持多种布尔表示
  • FormatInt指定进制格式化
  • FormatFloat指定格式和精度
  • Append系列追加到字节切片
  • Quote/Unquote处理字符串引号
  • Parse函数返回错误需检查
  • 进制0表示自动识别0x/0前缀

📝 发现内容有误?点击此处直接编辑

← 上一篇 Go os包
下一篇 → Go strings包
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库