Strconv package is the package that does type conversion in GO, can do various types and string type conversion.
Boolean value and string conversion
Convert Boolean values to strings using FormatBool
True converts to string true, false converts to string false
package main
import (
"fmt"
"strconv"
)
func main(a) {
t := true
f := false
fmt.Println(strconv.FormatBool(t))
fmt.Println(strconv.FormatBool(f))
}
Copy the code
Convert a string to a Boolean value using ParseBool
Boolean TRUE for string 1, t, t, TRUE, TRUE, False, False for string 0, f, f, False, False, False, False and all other values will return an error
package main
import (
"fmt"
"strconv"
)
func main(a) {
fmt.Println(strconv.ParseBool("1"))
fmt.Println(strconv.ParseBool("t"))
fmt.Println(strconv.ParseBool("T"))
fmt.Println(strconv.ParseBool("TRUE"))
fmt.Println(strconv.ParseBool("True"))
fmt.Println(strconv.ParseBool("true"))
fmt.Println(strconv.ParseBool("TrUe"))}Copy the code
Integer and string conversion
Convert numbers to strings
The FotmatInt method takes two parameters
func FormatInt(i int64, base int) string
Copy the code
The second is the cardinal number.
package main
import (
"fmt"
"strconv"
)
func main(a) {
v := int64(12)
fmt.Println(strconv.FormatInt(v, 10))
fmt.Println(strconv.FormatInt(v, 12))}Copy the code
In addition, there is a FormatUnit method that converts unsigned integers to strings, and the Itoa method Package Main is provided to facilitate common base 10 conversions
import ( “fmt” “strconv” )
Func main() {v := int64(98) fmt.println (strconv.itoa (98)) // equivalent to fmt.println (strconv.formatint (v, 10))}
Convert strings to numbers
ParseInt takes three arguments:
func ParseInt(s string, base int, bitSize int) (i int64, err error)
Copy the code
Base indicates a base (2 to 36). If base is 0, the value is determined by the string prefix (prefix :0x indicates hexadecimal; 0 indicates base 8. Otherwise, base 10), that is, what base to parse a given integer string; The bitSize argument represents the range of integer values, or the specific type of integer. Values 0, 8, 16, 32, and 64 represent int, int8, int16, int32, and int64 respectively.
If the string contains non-numbers, the conversion fails and an error is returned:
package main
import (
"fmt"
"strconv"
)
func main(a) {
v := "989898"
v2 := "9898abc"
fmt.Println(strconv.ParseInt(v, 10.32))
fmt.Println(strconv.ParseInt(v2, 10.32))}Copy the code
In addition, there is a ParseUnit method for converting a string to an unsigned integer, and an Atoi method is provided to facilitate common base 10 conversions
package main
import (
"fmt"
"strconv"
)
func main(a) {
v := "98"
fmt.Println(strconv.Atoi(v))
/ / equivalent to the
fmt.Println(strconv.ParseInt(v, 10.0))}Copy the code
Conversion of floating point and string
Converts floating point numbers to strings
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
Copy the code
F indicates a variable of the float64 type to be converted. FMT indicates that the value of f does not use the exponent. Prec indicates that the number of decimal places is reserved.
package main
import (
"strconv"
"fmt"
)
func main(a) {
var num float64 = 3.24
fmt.Println(strconv.FormatFloat(num, 'f'.10.64))}Copy the code
Converts a string to a floating point number
Use the ParseFloat
func ParseFloat(s string, bitSize int) (float64, error)
Copy the code
Where bitSize indicates whether to convert to float32 or float64:
package main
import (
"fmt"
"strconv"
)
func main(a) {
var s = "3.24"
fmt.Println(strconv.ParseFloat(s, 64))}Copy the code