SoFunction
Updated on 2025-03-05

Go basic data types and string types are transferred together

1. Basic data type to string type

Method 1: ("%parameter", expression)

1) Official explanation: Sprintf generates a formatted string based on the format parameter and returns the string.

func Sprintf(format string, a ...interface{}) string

2) Specific usage method:

① To string type

var num1 int = 99
str := ("%d", num1)

② Floating point type to string type

var num2 float64 = 23.456
str := ("%f", num2)

③ Boolean type to string type

var b bool = true
str := ("%t", b)

④ Character type (byte) to string type

var mychar byte = 'h'
str := ("%c", mychar)

Method 2: Functions using strconv package

func FormatBool(b bool) string
func FormatInt(i int64, base int) string
func FormatUint(i uint64, base int) string
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
func Itoa(i int) string

① To convert numeric type to string type (two ways)

var num1 int = 99
//The first parameter needs to be converted to int64 type, and the second parameter indicates the number of timesstr := (int64(num1), 10)
var num1 int = 99
str := (num1)

② Floating point type to string type

func FormatFloat(f float64, fmt byte, prec, bitSize int) string

f represents the floating point number to be converted

fmt represents formats: 'f' (-), 'b' (-dddp±ddd, exponent is binary), 'e' (-±dd, decimal index), 'E' (-±dd, decimal index), 'g' (used in 'e' format when the exponent is large, otherwise 'f' format), 'G' (used in 'E' format when the exponent is large, otherwise 'f' format).

prec controls accuracy (excluding exponential part): for 'f', 'e', ​​'E', it represents the number of numbers after the decimal point; for 'g', 'G', it controls the total number of numbers. If prec is -1, it means that f is represented by the minimum number of but necessary numbers.

bitSize represents the source type of f (32: float32, 64: float64), and will be rounded accordingly.

var num2 float64 = 23.456
str := (num2, 'f', 3, 64)

③ Boolean type to string type

var b bool = true
str := (b)

2. String type to basic data type

Functions using strconv package

① String type to Boolean type

var str string = "true"
b, _ := (str)

②Stand type to number type (two ways)

a.

func ParseInt(s string, base int, bitSize int) (i int64, err error)

Returns the integer value represented by the string, accepting a positive and negative sign.

base specifies the binary (2 to 36). If base is 0, it will be judged from the pre-story of the string. "0x" is hexadecimal, "0" is octal, otherwise it is decimal;

bitSize specifies that the result must be of no overflow of the integer type of assignment. 0, 8, 16, 32, 64 represent int, int, int8, int16, int32, and int64 respectively; the returned err is of type *NumErr, if the syntax is incorrect, = ErrSyntax; if the result exceeds the type range = ErrRange.

var str string = "1234"
n, _ := (str, 0, 64)

b.

var str string = "1234"
num, _ := (str)

③Stand type to floating point type

func ParseFloat(s string, bitSize int) (f float64, err error)

If s complies with syntax rules, the function returns a floating point number closest to the value of s (rounded using the IEEE754 specification). bitSize specifies the expected reception type, 32 is float32 (the return value can be assigned to float32 without changing the exact value), and 64 is float64; the return value err is of *NumErr type, if the syntax is incorrect, =ErrSyntax; if the result is beyond the representation range, the return value f is ±Inf, = ErrRange.

var str string = "123.456"
n, _ := (str, 64)

This is the end of this article about the transfer of Go basic data types and string types. For more related contents of Go basic data types and string types, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!