命令行通过flag参数来访问指定路径

zykzhang · · 1989 次点击
package main import ( "flag" "fmt" "log" "os" "os/user" ) var dir = flag.String("dir", "", "input a dir") func main() { flag.Parse() if len(*dir) == 0 { log.Fatal(nil) } outputDir := os.Expand(*dir, func(s string) string { if s == "~" { u, err := user.Current() if err != nil { return "" } return u.HomeDir } return "" }) fmt.Println(outputDir) }
#2
更多评论
这样如何: ```go package main import ( "flag" "fmt" "log" "os" "os/user" ) var dir = flag.String("dir", "", "input a dir") func main() { flag.Parse() if len(*dir) == 0 { log.Fatal(nil) } outputDir := os.Expand(*dir, func(s string) string { if s == "~" { u, err := user.Current() if err != nil { return "" } return u.HomeDir } return "" }) fmt.Println(outputDir) } ```
#1