[Go] Conversion between strings and basic data types

Table of contents

String conversion to other types

Convert other types to strings

Handling errors and defaults


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:

  1. Atoi(s string) (int, error): Convert the string to int type.

  2. Itoa(i int) string: Convert the int type to a string.

  3. ParseBool(str string) (bool, error): Convert string to bool type.

  4. FormatBool(b bool) string: Convert bool type to string.

  5. ParseInt(s string, base int, bitSize int) (int64, error): converts the string to the int64 type in the specified base.

  6. ParseUint(s string, base int, bitSize int) (uint64, error): Converts a string to an unsigned integer type (uint64).

  7. FormatInt(i int64, base int) string: Convert the int64 type to a string under the specified base.

  8. FormatUint(u uint64, base int) string: Convert the unsigned integer type (uint64) to a string in the specified base.

  9. ParseFloat(s string, bitSize int) (float64, error): Convert the string to float64 type.

  10. FormatFloat(f float64, fmt byte, prec, bitSize int) string: Convert the float64 type to a string, and the format and precision can be specified.

  11. AppendInt(dst []byte, i int64, base int) []byte: Append the int64 type to the byte slice.

  12. AppendBool(dst []byte, b bool) []byte: Append the bool type to the byte slice.

  13. AppendQuote(dst []byte, s string) []byte: Quote the string and append it to the byte slice.

strconvPackage 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 strconvexamples 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.AtoiConvert String to Integer, strconv.ParseBoolConvert String to Boolean, and strconv.ParseFloatConvert 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.ItoaConvert Integer to String, strconv.FormatBoolConvert Boolean to String, strconv.FormatFloatConvert 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 Atoithe error of the function and provided a default value as a fallback.

These examples are only strconvpart 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 strconvthe command to get more detailed information.

Guess you like

Origin blog.csdn.net/fanjufei123456/article/details/132099107