Different Ways to Convert the Boolean Type in String in Golang

In order to convert Boolean Type to String type in Golang , you can use the strconv and fmt package function.

1. strconv.FormatBool() Method: The FormatBool is used to Boolean Type to String. It returns “true” or “false” according to the value of b.

Syntax:

func FormatBool(b bool) string

Example : Convert an Boolean Type into String using strconv.FormatBool() function.

Go



filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate 
// How to convert the
// Boolean Type into String
package main
  
import (
    "fmt"
    "strconv"
)
  
//Main Function
  
func main() {
  
    i := true
    s := strconv.FormatBool(i)
    fmt.Printf("Type : %T \nValue : %v\n", s, s)
  
}

chevron_right


Output:

Type : string 
Value : true

2. fmt.Sprintf() Method: The Sprintf is used to represents the string by formatting according to a format specifier.

Syntax:

func Sprintf(format string, a ...interface{}) string

Example : Convert an Boolean Type into String using fmt.Sprintf() function.

Go

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate 
// How to convert the
// Boolean Type into String
package main
  
import (
    "fmt"
)
  
//Main Function
  
func main() {
  
    i := true
    s := fmt.Sprintf("%v", i)
    fmt.Printf("Type : %T \nValue : %v\n\n", s, s)
      
    i1 := false
    s1 := fmt.Sprintf("%v", i1)
    fmt.Printf("Type : %T \nValue : %v\n", s1, s1)
}

chevron_right


Output:

Type : string 
Value : true

Type : string 
Value : false



My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.