SoFunction
Updated on 2025-03-05

Understand Go language standard library strconv in one article

import "strconv"

The strconv package implements the mutual conversion of basic data types and their string representations.

string and int type conversion

Atoi()

func Atoi(s string) (i int, err error)

  • The Atoi() function is used to convert integers of string type to int type. If the passed string parameter cannot be converted to int type, an error will be returned.
  • This function is equivalent to ParseInt(str string, base int, bitSize int)

Example:

x1:= "123"
("Before:") 
("Type:%T ", x1) 
("\nValue:%v", x1) 
y1, e1:= (x1) 
if e1 == nil { 
  ("\nAfter:") 
  ("Type:%T ", y1) 
  ("\nValue:%v", y1) 
}

Output:

Before:
Type:string 
Value:123
After:
Type:int 
Value:123

Itoa()

func Itoa(i int) string

  • The Itoa() function is used to convert int type data into the corresponding string representation.
  • This function is equivalent to FormatInt(int64(x), 10). The Itoa() function returns a string representation of x when the cardinality is 10.

Example:

val:= int(123)
res:= (val)
("Result:%v", res) //Result:123
("\nType:%T", res) //Type:string

Parse series functions

ParseBool()

func ParseBool(str string) (value bool, err error)

Convert the specified string to the corresponding bool type, only accepts 1, 0, t, f, T, F, true, false, True, False, TRUE, FALSE, otherwise an error will be returned.

Example:

(("1"))  // true <nil>
(("F")) // false <nil>

ParseInt()、ParseUnit()

ParseInt() and ParseUint() have 3 parameters:

func ParseInt(s string, base int, bitSize int) (i int64, err error)
func ParseUint(s string, base int, bitSize int) (uint64, error)
  • The base parameter indicates in which the given string is parsed in a binary way, with valid values ​​of 0, 2-36. When base=0, it means to judge which department is used to parse according to the prefix of string: 0x is parsed in hexadecimal, 0 is parsed in angular, and others are parsed in decimal.

  • The bitSize parameter indicates the int/uint of the conversion bit, and the valid values ​​are 0, 8, 16, 32, 64. When bitSize=0, it means it is converted to int or uint type. For example, bitSize=8 means that the converted value is int8 or uint8.

  • Returns the integer value represented by the string, ParseInt(), accepts a positive and negative sign. ParseUint does not accept positive and negative signs and is used for unsigned integers.

Example:

//Parse "-23" in vernal mode and save it as int64 type:i, _ := ("23", 5, 64)
//Parse "23" in hexadecimal mode and save it as int64 type:u, _ := ("23", 16, 64)
(i)  // -13
(u) // 35

ParseFloat()

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

Parses a string representing a floating point number and returns its value.

If s complies with syntax rules, the function returns a floating point number closest to the value of s (usingIEEE754standard rounding).

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;

Return value err Yes*NumErrType, if syntax is incorrect, =ErrSyntax; if the result is outside the representation range, the return value f is ±Inf, = ErrRange.

f, _ := ("3.1415", 32)
(f)  //3.1414999961853027

Format series functions

The Format series functions implement the function of formatting the given type of data into string type data.

FormatBool()

func FormatBool(b bool) string

Returns "true" or "false" based on the value of b.

FormatInt()、FormatUint()

FormatInt() and FormatUint() have two parameters:

func FormatInt(i int64, base int) string
func FormatUint(i uint64, base int) string
  • The second parameter base specifies how much to convert the first parameter to, with a valid value of 2<=base<=36. When the specified bit is greater than 10, the value exceeding 10 is represented by a-z letters. For example, in hexadecimal, the numbers of 10-15 are represented by a-f, and in hexadecimal, the values ​​of 10-16 are represented by a-g.

  • Returns the string representation of the base of i, FormatUint is the unsigned integer version of FormatInt.

Example:

((-42, 16)) //-2a
((42, 16)) //2a

FormatFloat()

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

  • The function represents a floating point number as a string and returns.

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

  • fmt represents formats: 'f' (-), 'b' (-dddp±ddd, exponent is binary), 'e' (-±dd, decimal index), 'E' (-±dd, decimal index), 'g' (using 'e' format when the exponent is large, otherwise 'f' format), 'G' (using '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.

Example:

//3.1415E+00
((3.1415, 'E', -1, 64))

other

This article introduces some commonly used functions of the strconv package. For more information about other functions, please check the official documentation:/pkg/strconv/

This is the end of this article about understanding the Go language standard library strconv in one article. For more related Go strconv content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!