Table of contents
String conversion to other types
Convert other types to strings
In the strconv package of the Go language, a series of functions for converting between strings and basic data types are provided. The following is a list of the main functions in the strconv package:
Atoi(s string) (int, error): Convert the string to int type.
Itoa(i int) string: Convert the int type to a string.
ParseBool(str string) (bool, error): Convert string to bool type.
FormatBool(b bool) string: Convert bool type to string.
ParseInt(s string, base int, bitSize int) (int64, error): converts the string to the int64 type in the specified base.
ParseUint(s string, base int, bitSize int) (uint64, error): Converts a string to an unsigned integer type (uint64).
FormatInt(i int64, base int) string: Convert the int64 type to a string under the specified base.
FormatUint(u uint64, base int) string: Convert the unsigned integer type (uint64) to a string in the specified base.
ParseFloat(s string, bitSize int) (float64, error): Convert the string to float64 type.
FormatFloat(f float64, fmt byte, prec, bitSize int) string: Convert the float64 type to a string, and the format and precision can be specified.
AppendInt(dst []byte, i int64, base int) []byte: Append the int64 type to the byte slice.
AppendBool(dst []byte, b bool) []byte: Append the bool type to the byte slice.
AppendQuote(dst []byte, s string) []byte: Quote the string and append it to the byte slice.
strconv
Package is one of the standard libraries in the Go language for conversion between strings and primitive data types. It provides many functions for convenient string parsing and formatting. Here are some strconv
examples of common usage of packages:
String conversion to other types
package main
import (
"fmt"
"strconv"
)
func main() {
str := "1234"
// 字符串转换为整数
num, err := strconv.Atoi(str)
if err != nil {
fmt.Println("转换失败:", err)
return
}
fmt.Println(num)
// 字符串转换为布尔值
b, err := strconv.ParseBool("true")
if err != nil {
fmt.Println("转换失败:", err)
return
}
fmt.Println(b)
// 字符串转换为浮点数
f, err := strconv.ParseFloat("3.14", 64)
if err != nil {
fmt.Println("转换失败:", err)
return
}
fmt.Println(f)
}
In the above examples, we used strconv.Atoi
Convert String to Integer, strconv.ParseBool
Convert String to Boolean, and strconv.ParseFloat
Convert String to Float. If an error occurs during conversion, we print an error message.
Convert other types to strings
package main
import (
"fmt"
"strconv"
)
func main() {
num := 1234
// 整数转换为字符串
str := strconv.Itoa(num)
fmt.Println(str)
// 布尔值转换为字符串
str = strconv.FormatBool(true)
fmt.Println(str)
// 浮点数转换为字符串
str = strconv.FormatFloat(3.14, 'E', -1, 64)
fmt.Println(str)
}
In the above examples, we used strconv.Itoa
Convert Integer to String, strconv.FormatBool
Convert Boolean to String, strconv.FormatFloat
Convert Float to String, and specify the print format.
Handling errors and defaults
package main
import (
"fmt"
"strconv"
)
func main() {
str := "abc"
// 错误处理
num, err := strconv.Atoi(str)
if err != nil {
fmt.Println("转换失败:", err)
} else {
fmt.Println(num)
}
// 默认值
num, err = strconv.Atoi(str)
if err != nil {
num = 0 // 默认值
}
fmt.Println(num)
}
In the above example, we handled Atoi
the error of the function and provided a default value as a fallback.
These examples are only strconv
part of the package, which also provides many other functions to perform more complex conversion operations. You can consult the official documentation or use go doc strconv
the command to get more detailed information.