How to convert string to boolean in Go

3 Answers

0 votes
package main

import (
	"fmt"
	"reflect"
	"strconv"
)

func main() {
    s := "Golang"
  
    fmt.Println(reflect.TypeOf(s)) 

    b, _ := strconv.ParseBool(s) 
    fmt.Println(b)
    fmt.Println(reflect.TypeOf(b)) 
}




/*
run:

string
false
bool

*/

 



answered May 26, 2020 by avibootz
0 votes
package main

import (
	"fmt"
	"reflect"
	"strconv"
)

func main() {
    s := "true"
  
    fmt.Println(reflect.TypeOf(s)) 

    b, _ := strconv.ParseBool(s) 
    fmt.Println(b)
    fmt.Println(reflect.TypeOf(b)) 
}




/*
run:

string
true
bool

*/

 



answered May 26, 2020 by avibootz
0 votes
package main

import (
	"fmt"
	"strconv"
)

func main() {
     b, _ := strconv.ParseBool("false")

     fmt.Printf("%T\n", b)
     fmt.Println(b)
}




/*
run:

bool
false

*/

 



answered May 26, 2020 by avibootz
...