Usage of the Go type conversion toolkit strconv package
Go languagestrconv
The package provides functions for converting between basic data types, including string to other basic types conversions, as well as other basic types to string conversions.
Convert strings to basic data types
-
: Convert string to
int
-
: Convert string to
bool
-
: Convert string to
float64
-
: Convert string to
int64
, you can specify the binary system -
: Convert string to
uint64
, you can specify the binary system
package main import ( "fmt" "log" "strconv" ) func main() { //Convert string to basic data type //----------------------------------------------------------------------------------------------------------------------------- intStr := "123" intVal, err := (intStr) if err != nil { (err) } ("%v,%T\n", intVal, intVal) (intVal) // Output: 123 //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- boolStr := "true" boolVal, err := (boolStr) if err != nil { (err) } ("%v,%T\n", boolVal, boolVal) (boolVal) // Output: true //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- floatStr := "123.45" floatVal, err := (floatStr, 64) if err != nil { (err) } ("%v,%T\n", floatVal, floatVal) (floatVal) // Output: 123.45 //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- intStrParseInt := "123" // Default is decimal intValParseInt, err := (intStrParseInt, 10, 64) if err != nil { (err) } ("%v,%T\n", intValParseInt, intValParseInt) (intValParseInt) // Output: 123 //----------------------------------------------------------------------------------------------------------------------------- binStr := "11010001" var intVal2, err2 = (binStr, 2, 64) if err2 != nil { (err2) } ("%v,%T\n", intVal2, intVal2) (intVal2) // Output: 209 //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- uintStr := "123" uintVal, err := (uintStr, 10, 64) if err != nil { (err) } ("%v,%T\n", uintVal, uintVal) (uintVal) // Output: 123}
Convert basic data type to string
-
:Will
int
Convert to string -
:Will
bool
Convert to string -
:Will
float64
Convert to a string, you can specify formatting options -
:Will
int64
Convert to a string, you can specify the binary system -
:Will
uint64
Convert to a string, you can specify the binary system
package main import ( "fmt" "strconv" ) func main() { //Convert basic data type to string //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- intVal := 123 intStr := (intVal) ("%v,%T\n", intStr, intStr) (intStr) // Output: 123 // - Convert bool to string. boolVal := true boolStr := (boolVal) ("%v,%T\n", boolStr, boolStr) (boolStr) // Output: true //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- floatVal := 123.45 floatStr := (floatVal, 'f', 2, 64) ("%v,%T\n", floatStr, floatStr) (floatStr) // Output: 123.45 //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- intVal64 := int64(123) intStr64 := (intVal64, 10) // Decimal ("%v,%T\n", intStr64, intStr64) (intStr64) // Output: 123 //----------------------------------------------------------------------------------------------------------------------------- intStr2 := (intVal64, 2) ("%v,%T\n", intStr2, intStr2) (intStr) // Output: 1111011 //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- uintVal := uint64(123) uintStr := (uintVal, 10) // Decimal ("%v,%T\n", uintStr, uintStr) (uintStr) // Output: 123 //----------------------------------------------------------------------------------------------------------------------------- uintStr = (uintVal, 16) ("%v,%T\n", uintStr, uintStr) (uintStr) // Output: 7b}
This is the end of this article about the usage of the Go type conversion toolkit strconv package. For more related content of Go strconv package, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!
Related Articles
Case Study on Blockchain Development Using Web3 Library in Go
As a distributed ledger technology, blockchain has made great progress in recent years. Golang is an efficient and highly concurrent programming language, and is widely used in blockchain development. This article will introduce how to use the Web3 library in Golang for blockchain development and provide some practical cases. Friends who need it can refer to it.2023-10-10Operation of converting interface{} into array in Golang
This article mainly introduces the operation of converting interface{} into array in Golang, which has good reference value and hopes it will be helpful to everyone. Let's take a look with the editor2021-04-04Go gRPC server streaming RPC tutorial example
This article mainly introduces Go gRPC server streaming RPC tutorial examples. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get a promotion as soon as possible.2022-06-06Detailed explanation of examples of Golang implementing combination mode and decorative mode
This article mainly introduces Golang to implement combination mode and decoration mode. This article introduces combination mode and decoration mode. Golang implements two modes in common, but there are differences in specific application scenarios. By comparing the two modes, you can deepen your understanding. Friends who need it can refer to it.2022-11-11Go iota keyword and enum type implementation principle
This article mainly introduces the implementation principles of Go iota keywords and enumeration types. iota is a constant counter of the Go language and can only be used in constant expressions. For more related content, please refer to it.2022-07-07Apache IoTDB development system Go native interface method
This article mainly introduces the Go native interface method of the Apache IoTDB development system. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get a promotion as soon as possible.2023-09-09go Read BMP file header binary reading method
This article mainly introduces the binary reading method of go to read BMP file header, which is of good reference value and hopes to be helpful to everyone. Let's take a look with the editor2020-12-12Introduction to the difference between copy transfer of structure method in Go language and participating in pointer transfer parameters
This article mainly introduces relevant information to you about the difference between copy-transmission of structure methods in Go language and pointer parameters. In the article, it first provides some brief introduction to the difference between structure methods in GO language and structure pointer methods to help everyone understand and learn. Friends who need it can refer to it.2017-12-12Golang calls C++ library source code example through cgo
This article mainly introduces relevant information about golang calling the C++ library through cgo. CGO is a feature in the GO language. CGO belongs to the advanced usage of GOLANG, mainly through the use of GOLANG to call CLANG. Friends who need it can refer to it.2024-02-02A detailed analysis of the difference between type and kind in Go reflection
This article mainly introduces relevant information about the difference between type and kind in Go reflection. Type is the interface type, Value is the Struct type, Type is the type description, and Value is the specific value. Friends who need it can refer to it.2023-10-10