Converting a string variable into Boolean, Integer or Float type in Golang
Various types of string conversions are needed while performing tasks in Golang. Package strconv is imported to perform conversions to and from string.
String to Boolean Conversion
ParseBool is used to convert string to boolean value. It accepts 1, t, T, TRUE, true, True as true and 0, f, F, FALSE, false, False as false. Any other value returns an error and will display the value as false.
Example:
// Golang program to convert a string // into Boolean data type package main import ( "fmt" "reflect" "strconv" ) func main() { str := "w3wiki" fmt.Println( "Before :" , reflect.TypeOf(str)) fmt.Println( "String value is: " , str) b, _ := strconv.ParseBool(str) fmt.Println( "After :" , reflect.TypeOf(b)) fmt.Println( "Boolean value is: " , b, "\n" ) str1 := "t" fmt.Println( "Before :" , reflect.TypeOf(str1)) fmt.Println( "String value is: " , str1) b1, _ := strconv.ParseBool(str1) fmt.Println( "After :" , reflect.TypeOf(b1)) fmt.Println( "Boolean value is: " , b1, "\n" ) str2 := "0" fmt.Println( "Before :" , reflect.TypeOf(str2)) fmt.Println( "String value is: " , str2) b2, _ := strconv.ParseBool(str2) fmt.Println( "After :" , reflect.TypeOf(b2)) fmt.Println( "Boolean value is: " , b2, "\n" ) } |
Output:
Before : string String value is: w3wiki After : bool Boolean value is: false Before : string String value is: t After : bool Boolean value is: true Before : string String value is: 0 After : bool Boolean value is: false
String to Integer Conversion
ParseInt function is used to convert string to an integer value. It interprets a string in the given base (0, 2 to 36) and bit size (0 to 64) and returns the corresponding value.
Example:
// Golang program to convert String // into Integer Data type package main import ( "fmt" "reflect" "strconv" ) func main() { str := "w3wiki" fmt.Println( "Before :" , reflect.TypeOf(str)) fmt.Println( "String value is: " , str) i, _ := strconv.ParseInt(str, 10, 64) fmt.Println( "After :" , reflect.TypeOf(i)) fmt.Println( "Integer value is: " , i, "\n" ) str1 := "100" fmt.Println( "Before :" , reflect.TypeOf(str1)) fmt.Println( "String value is: " , str1) i1, _ := strconv.ParseInt(str1, 10, 64) fmt.Println( "After :" , reflect.TypeOf(i1)) fmt.Println( "Integer value is: " , i1, "\n" ) } |
Output:
Before : string String value is: w3wiki After : int64 Integer value is: 0 Before : string String value is: 100 After : int64 Integer value is: 100
String to Float Conversion
ParseFloat is used to convert the string to float type with the precision specified by bitSize: 32 for float32, or 64 for float64. When bitSize=32, the result still has type float64, but it will be convertible to float32 without changing its value.
Example:
// Golang program to convert // String into Float Data type package main import ( "fmt" "reflect" "strconv" ) func main() { str := "3.1415926535" fmt.Println( "Before :" , reflect.TypeOf(str)) fmt.Println( "String value is: " , str) f, _ := strconv.ParseFloat(str, 64) fmt.Println( "After :" , reflect.TypeOf(f)) fmt.Println( "Float value is: " , f, "\n" ) str1 := "3.1415926535" fmt.Println( "Before :" , reflect.TypeOf(str1)) fmt.Println( "String value is: " , str1) f1, _ := strconv.ParseFloat(str1, 32) fmt.Println( "After :" , reflect.TypeOf(f1)) fmt.Println( "Float value is: " , f1, "\n" ) } |
Output:
Before : string String value is: 3.1415926535 After : float64 Float value is: 3.1415926535 Before : string String value is: 3.1415926535 After : float64 Float value is: 3.1415927410125732
Contact Us