我弄乱了标志库,发现这段代码不起作用:
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 \n\n", recursive)
flag.PrintDefaults()
}
但这确实(我评论了我更改的三行):
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 \n\n", *recursive) //Changed to dereference variable
flag.PrintDefaults()
}
为什么会有这样的行为?不允许在 Golang 中取消引用函数,还是我做错了什么?
慕码人8056858
相关分类