SoFunction
Updated on 2025-03-05

Implementation method of converting int type and string type in Golang

Use strconv package

The Itoa and Atoi functions provided by the strconv package can be used to convert each other between int type and string type.

1. int to string

package main
import (
	"fmt"
	"strconv"
)
func main() {
	num := 123
	str := (num)
	(str) // Output: "123"}

Use the Itoa function to convert the int type to a string type. The method of using the Itoa function is very simple. You just need to pass the int value to be converted into the function and return the converted string.

2. String to int

package main
import (
	"fmt"
	"strconv"
)
func main() {
	str := "456"
	num, err := (str)
	if err != nil {
		("Conversion failed:", err)
	} else {
		(num) // Output: 456	}
}

Use the Atoi function to convert the string type to an int type. You only need to pass the string into the function, returning the converted int value and possible error information.

Use the fmt package

The fmt package also provides some functions that can complete the conversion between int type and string type.

1. int to string

package main
import (
	"fmt"
)
func main() {
	num := 123
	str := ("%d", num)
	(str) // Output: "123"}

Use the Sprintf function in the fmt package to return the formatted string, and pass in a placeholder "%d" and the int value to be converted to the function.

2. String to int

package main
import (
	"fmt"
)
func main() {
	str := "456"
	var num int
	_, err := (str, "%d", &num)
	if err != nil {
		("Conversion failed:", err)
	} else {
		(num) // Output: 456	}
}

Use the Sscanf function in the fmt package to parse a string into an int value based on the specified format. Pass the string to be parsed, the placeholder "%d" and a pointer to the int variable to the function, and parse the corresponding int value from the string and assign it to the specified variable.

Use the /spf13/cast library

The cast library is a library used to convert between different types. It has very powerful functions and naturally includes the mutual conversion function between int type and string type.

1. int to string

package main
import (
	"fmt"
	"/spf13/cast"
)
func main() {
	num := 123
	str := (num)
	(str) // Output: "123"}

Use the ToString function to convert other types to string types, just pass in the value to be converted.

2. String to int

package main
import (
	"fmt"
	"/spf13/cast"
)
func main() {
	str := "456"
	num := (str) // Output: 456	(num)
}

Use the ToInt function to convert other types to int type, just pass in the value to be converted.

package main
import (
	"fmt"
	"/spf13/cast"
)
func main() {
	str := "Ludoshin's Blog"
	num := (str) // // Output: 0	(num)
}

summary

This article introduces in detail several methods in Golang to convert int type and string type. I hope these methods can make you more comfortable when using Golang.

The above is the detailed content of the implementation method of converting int types and string types in Golang. For more information on converting Golang int types and string types, please pay attention to my other related articles!