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

Yasir ArafatYasir Arafat
3 min read

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 := "GeeksforGeeks"

    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:  GeeksforGeeks
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 := "GeeksforGeeks"

    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")

    // parsing the string "100" to int in decimal system (base 10)

    str1 := "100"

    fmt.Println("Before :", reflect.TypeOf(str1))
    fmt.Println("String value is: ", str1)
    i1, _ := strconv.ParseInt(str1, 10, 64) // the base is given as 10 and size for int is 64 bits
    fmt.Println("After :", reflect.TypeOf(i1))
    fmt.Println("Integer value is: ", i1, "\\n")

    // parsing the string "1011" to int in binary system (base 2)
    str2 := "1011"

    fmt.Println("Before :", reflect.TypeOf(str2))
    fmt.Println("String value is: ", str2)
    i2, _ := strconv.ParseInt(str2, 2, 64) // the base is given as 2 and size for int is 64 bits
    fmt.Println("After :", reflect.TypeOf(i2))
    fmt.Println("Integer value is: ", i2, "\\n")
}

Output:

Before : string
String value is:  GeeksforGeeks
After : int64
Integer value is:  0 

Before : string
String value is:  100
After : int64
Integer value is:  100 

Before : string
String value is:  1011
After : int64
Integer value is:  11

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) // the result will be float64 type
    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) // the result will be still of float64 type
    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
0
Subscribe to my newsletter

Read articles from Yasir Arafat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yasir Arafat
Yasir Arafat

๐Ÿ‘‹ Hello there! I'm Yasir Arafat, a motivated 3rd-year undergraduate at IIIT-Allahabad, driven by a passion for Data Structures and Algorithms (DSA) and Web Development. ๐ŸŒŸ Achievements: Currently holding the esteemed title of a Knight at Leetcode and Specialist at Codeforces. Consistently achieving ranks near 1800 in various DSA/CC contests. ๐Ÿ’ป Technical Expertise: In-depth understanding of the MERN stack, with a focus on backend development. Noteworthy projects include the creation of a Virtual Trading Platform and a Student Portfolio Management system. Knowledgeable in Object-Oriented Programming (OOP), Database Management Systems (DBMS), and Operating Systems (OS). Proficient in Git version control. ๐Ÿš€ Outlook: Excited about the convergence of technology and creativity, I am open to new opportunities and collaborations.