I'm messing with the flag library, and found that this code does not work:
package main
import (
"fmt"
"flag"
)
var recursive bool
func init() {
recursive = *flag.Bool("r", false, "Search recursively")
}
func main() {
flag.Parse()
fmt.Printf("Recursive: %t
", recursive)
flag.PrintDefaults()
}
But this does (I commented the three lines I changed):
package main
import (
"fmt"
"flag"
)
var recursive *bool //Changed to pointer type
func init() {
recursive = flag.Bool("r", false, "Search recursively") //Changed to not dereference function
}
func main() {
flag.Parse()
fmt.Printf("Recursive: %t
", *recursive) //Changed to dereference variable
flag.PrintDefaults()
}
Why does this behave like this? Are functions not allowed to be dereferenced in Golang, or am I doing something else wrong?