golang標準庫strconv常用方法
strconv 包是用來處理字符串和基本數(shù)據(jù)類型之間轉(zhuǎn)換的。它提供了多種函數(shù),用于將字符串轉(zhuǎn)換為數(shù)字類型(如整型、浮點型等),以及將數(shù)字類型轉(zhuǎn)換為字符串。
常用方法
字符串與數(shù)字轉(zhuǎn)換
字符串轉(zhuǎn)整數(shù)
// 字符串轉(zhuǎn) int
num, err := strconv.Atoi("123") // 123, nil
num, err = strconv.Atoi("abc") // 0, error
// 字符串轉(zhuǎn) int(指定進制和位數(shù))
num64, err := strconv.ParseInt("123", 10, 64) // int64 123, nil
num64, err = strconv.ParseInt("FF", 16, 64) // int64 255, nil
num64, err = strconv.ParseInt("1010", 2, 64) // int64 10, nil
// 字符串轉(zhuǎn) uint
u, err := strconv.ParseUint("123", 10, 64) // uint64 123, nil整數(shù)轉(zhuǎn)字符串
// int 轉(zhuǎn)字符串 str := strconv.Itoa(123) // "123" // int64 轉(zhuǎn)字符串(指定進制) str = strconv.FormatInt(123, 10) // "123" (10進制) str = strconv.FormatInt(255, 16) // "ff" (16進制) str = strconv.FormatInt(10, 2) // "1010" (2進制) // uint64 轉(zhuǎn)字符串 str = strconv.FormatUint(123, 10) // "123"
字符串與浮點數(shù)轉(zhuǎn)換
字符串轉(zhuǎn)浮點數(shù)
// 字符串轉(zhuǎn) float64
f, err := strconv.ParseFloat("3.14", 64) // float64 3.14, nil
f, err = strconv.ParseFloat("1.23e-4", 64) // float64 0.000123, nil
f, err = strconv.ParseFloat("NaN", 64) // float64 NaN, nil
f, err = strconv.ParseFloat("Inf", 64) // float64 +Inf, nil
浮點數(shù)轉(zhuǎn)字符串
// float64 轉(zhuǎn)字符串 str := strconv.FormatFloat(3.14, 'f', 2, 64) // "3.14" str = strconv.FormatFloat(3.14159, 'f', -1, 64) // "3.14159" str = strconv.FormatFloat(3.14, 'e', 2, 64) // "3.14e+00" str = strconv.FormatFloat(123.456, 'E', -1, 64) // "1.23456E+02" // FormatFloat 格式說明: // 'f': 普通小數(shù)格式 (-ddd.dddd) // 'e': 科學計數(shù)法 (-d.dddde±dd) // 'E': 科學計數(shù)法 (-d.ddddE±dd) // 'g': 自動選擇 %e 或 %f // 'G': 自動選擇 %E 或 %f
字符串與布爾值轉(zhuǎn)換
字符串轉(zhuǎn)布爾值
// 字符串轉(zhuǎn) bool
b, err := strconv.ParseBool("true") // true, nil
b, err = strconv.ParseBool("1") // true, nil
b, err = strconv.ParseBool("t") // true, nil
b, err = strconv.ParseBool("TRUE") // true, nil
b, err = strconv.ParseBool("false") // false, nil
b, err = strconv.ParseBool("0") // false, nil
b, err = strconv.ParseBool("f") // false, nil
b, err = strconv.ParseBool("FALSE") // false, nil
b, err = strconv.ParseBool("abc") // false, error布爾值轉(zhuǎn)字符串
// bool 轉(zhuǎn)字符串 str := strconv.FormatBool(true) // "true" str = strconv.FormatBool(false) // "false"
引號處理
添加和去除引號
// 添加引號
quoted := strconv.Quote("Hello, 世界") // `"Hello, 世界"`
quoted = strconv.Quote(`She said "hello"`) // `"She said \"hello\""`
// 添加 ASCII 引號(非ASCII字符轉(zhuǎn)義)
quoted = strconv.QuoteToASCII("Hello, 世界") // `"Hello, \u4e16\u754c"`
// 添加字符引號
quoted = strconv.QuoteRune('世') // `'世'`
quoted = strconv.QuoteRuneToASCII('世') // `'\u4e16'`
// 去除引號
unquoted, err := strconv.Unquote(`"Hello"`) // "Hello", nil
unquoted, err = strconv.Unquote(`'世'`) // "世", nil
unquoted, err = strconv.Unquote("`raw string`") // "raw string", nil工具函數(shù)
帶錯誤檢查的轉(zhuǎn)換
// 安全的字符串轉(zhuǎn)整數(shù)
func SafeAtoi(s string, defaultValue int) int {
if s == "" {
return defaultValue
}
if num, err := strconv.Atoi(s); err == nil {
return num
}
return defaultValue
}
// 安全的字符串轉(zhuǎn)浮點數(shù)
func SafeParseFloat(s string, defaultValue float64) float64 {
if s == "" {
return defaultValue
}
if f, err := strconv.ParseFloat(s, 64); err == nil {
return f
}
return defaultValue
}
// 使用
age := SafeAtoi("25", 0) // 25
price := SafeParseFloat("19.99", 0.0) // 19.99
invalid := SafeAtoi("abc", -1) // -1進制轉(zhuǎn)換工具
// 不同進制轉(zhuǎn)換工具
func ConvertBase(num string, fromBase, toBase int) (string, error) {
// 先轉(zhuǎn)換為 int64
n, err := strconv.ParseInt(num, fromBase, 64)
if err != nil {
return "", err
}
// 再轉(zhuǎn)換為目標進制
return strconv.FormatInt(n, toBase), nil
}
// 使用
result, _ := ConvertBase("255", 10, 16) // "ff"
result, _ = ConvertBase("1010", 2, 10) // "10"
result, _ = ConvertBase("FF", 16, 2) // "11111111"生成帶引號的JSON值
func GenerateJSONString(value interface{}) string {
switch v := value.(type) {
case string:
return strconv.Quote(v)
case int:
return strconv.Itoa(v)
case float64:
return strconv.FormatFloat(v, 'f', -1, 64)
case bool:
return strconv.FormatBool(v)
default:
return strconv.Quote(fmt.Sprintf("%v", v))
}
}
// 使用
fmt.Println(GenerateJSONString("hello")) // "\"hello\""
fmt.Println(GenerateJSONString(123)) // "123"
fmt.Println(GenerateJSONString(3.14)) // "3.14"
fmt.Println(GenerateJSONString(true)) // "true"性能優(yōu)化的數(shù)字轉(zhuǎn)換
// 高性能的數(shù)字轉(zhuǎn)字符串(避免內(nèi)存分配)
type NumberBuffer struct {
buf []byte
}
func (b *NumberBuffer) Reset() {
b.buf = b.buf[:0]
}
func (b *NumberBuffer) AppendInt(n int) {
b.buf = strconv.AppendInt(b.buf, int64(n), 10)
}
func (b *NumberBuffer) AppendFloat(f float64) {
b.buf = strconv.AppendFloat(b.buf, f, 'f', 2, 64)
}
func (b *NumberBuffer) String() string {
return string(b.buf)
}
// 使用
var buf NumberBuffer
buf.AppendInt(123)
buf.AppendFloat(45.67)
result := buf.String() // "12345.67"總結(jié)
- 整數(shù)轉(zhuǎn)換 - Atoi, ParseInt, FormatInt, Itoa
- 浮點數(shù)轉(zhuǎn)換 - ParseFloat, FormatFloat
- 布爾值轉(zhuǎn)換 - ParseBool, FormatBool
- 引號處理 - Quote, Unquote, QuoteRune
到此這篇關(guān)于golang標準庫strconv的文章就介紹到這了,更多相關(guān)golang 標準庫strconv內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語言中Redis緩存與本地內(nèi)存緩存實戰(zhàn)
在現(xiàn)代高并發(fā)系統(tǒng)中,緩存技術(shù)是提升性能和降低數(shù)據(jù)庫壓力的關(guān)鍵手段,本文將為大家介紹一下Redis緩存與本地內(nèi)存緩存的具體應用,需要的可以了解下2025-03-03
用gin開發(fā)的golang項目三種開發(fā)模式方式
這篇文章主要介紹了用gin開發(fā)的golang項目三種開發(fā)模式方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
golang實現(xiàn)sql結(jié)果集以json格式輸出的方法
這篇文章主要介紹了golang實現(xiàn)sql結(jié)果集以json格式輸出的方法,涉及Go語言針對sql結(jié)果集的遍歷、轉(zhuǎn)換及json格式相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Go 循環(huán)結(jié)構(gòu)for循環(huán)使用教程全面講解
這篇文章主要為大家介紹了Go 循環(huán)結(jié)構(gòu)for循環(huán)使用全面講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10

