I am trying to setup a bool variable with a default value and update it based on a condition, in Go Lang. The func foo compiles, But the function bar doesn't compile and gives an error "f declared and not used"
There is a related answer - which doesn't explain the next question
What is the correct pattern for this(bar function) in Go?
Here is the code:
package main
import (
"fmt"
"strconv"
)
func foo(m map[string]string) bool {
f := false
if _, exists := m["READWRITE"]; exists {
fmt.Println("Before Updating f : ", f)
f, _ = strconv.ParseBool(m["READWRITE"])
//if err != nil {
// panic(err)
//}
}
fmt.Println("After Updating f : ", f)
return f
}
func bar(m map[string]string) bool {
f := false
if _, exists := m["READWRITE"]; exists {
fmt.Println("Before Updating f : ", f)
f, err := strconv.ParseBool(m["READWRITE"]) // error on this line "f declared and not used"
if err != nil {
panic(err)
}
}
fmt.Println("After Updating f : ", f)
return f
}
func main() {
m := map[string]string{"READWRITE": "true"}
fmt.Println(foo(m))
fmt.Println(bar(m))
}