Converting a string variable into Boolean, Integer or Float type in Golang


Converting a string variable into Boolean, Integer, or Float type is a common requirement in Golang programming. Golang provides built-in functions that can be used for this purpose. In this article, we will explore how to convert a string variable into a Boolean, Integer, or Float type in Golang.

Converting a String to a Boolean in Golang

To convert a string variable into a Boolean type, we can use the strconv.ParseBool() function. The ParseBool() function returns two values - the Boolean value and an error. The following code demonstrates how to convert a string variable into a Boolean type −

Example

package main

import (
   "fmt"
   "strconv"
)

func main() {
   str := "true"
   b, err := strconv.ParseBool(str)
   if err == nil {
      fmt.Println(b)
   }
}

Output

true

In the above code, we first declared a string variable named str and assigned a value of "true" to it. We then used the strconv.ParseBool() function to convert the string variable into a Boolean type. The ParseBool() function returns two values - the Boolean value and an error. We then printed the Boolean value to the console.

Converting a String to an Integer in Golang

To convert a string variable into an Integer type, we can use the strconv.Atoi() function. The Atoi() function returns two values - the Integer value and an error. The following code demonstrates how to convert a string variable into an Integer type −

Example

package main

import (
   "fmt"
   "strconv"
)

func main() {
   str := "123"
   i, err := strconv.Atoi(str)
   if err == nil {
      fmt.Println(i)
   }
}

Output

123

In the above code, we first declared a string variable named str and assigned a value of "123" to it. We then used the strconv.Atoi() function to convert the string variable into an Integer type. The Atoi() function returns two values - the Integer value and an error. We then printed the Integer value to the console.

Converting a String to a Float in Golang

To convert a string variable into a Float type, we can use the strconv.ParseFloat() function. The ParseFloat() function returns two values - the Float value and an error. The following code demonstrates how to convert a string variable into a Float type −

Example

package main

import (
   "fmt"
   "strconv"
)

func main() {
   str := "3.14"
   f, err := strconv.ParseFloat(str, 64)
   if err == nil {
      fmt.Println(f)
   }
}

Output

3.14

In the above code, we first declared a string variable named str and assigned a value of "3.14" to it. We then used the strconv.ParseFloat() function to convert the string variable into a Float type. The ParseFloat() function returns two values - the Float value and an error. We then printed the Float value to the console.

Conclusion

In this article, we learned how to convert a string variable into a Boolean, Integer, or Float type in Golang. Golang provides built-in functions such as strconv.ParseBool(), strconv.Atoi(), and strconv.ParseFloat() that can be used to perform these conversions. By using these functions, we can easily convert a string variable into the desired type.

Updated on: 07-Apr-2023

232 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements