原因分析 当然通过命令模式也会发现这样蛋疼的问题,go build不报错,直接go run main.go就报错。 这事你得揪其因呀,出现蛋疼问题就得抚平咯,哈哈哈。 在源代码的main函数中,我们发现从base.Commands的切片中获取要执行的命令,然后和传入的args一起执行cmd.Run(cmd, args)这个方法; 然后再回过头看cmd.
在源代码的main函数中,我们发现从base.Commands的切片中获取要执行的命令,然后和传入的args一起执行cmd.Run(cmd, args)这个方法; 然后再回过头看cmd.Run(cmd, args)这个函数,结果发现它只是定义了一种类型,具体实现这里没有指出; 紧接着回头去看run包下的函数,会发现run.go在初始化的时候,会把改文件下的runRu...
Command-line arguments are a common way to parameterize execution of programs. For example, go run hello.go uses run and hello.go arguments to the go program package main import ("fmt""os") func main() { argsWithProg :=os.Args argsWithoutProg := os.Args[1:] arg := os.Args[3] f...
在Go语言中,遇到“package command-line-arguments is not a main package”这个错误通常意味着你尝试运行的Go程序中没有包含一个名为main的包,或者你的程序结构不正确。下面我将详细解释这个错误的含义,并提供几种可能的解决方法。 1. 错误含义 在Go语言中,每个可执行程序都必须有一个名为main的包,并且这个包中...
很简单的一段代码,go运行一个http服务: package main import ( "io" "log" "net/http" ) func helloHandler(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "Hello, world!") } func main() { http.HandleFunc("/hello", helloHandler) ...
Golang (gomr) To build the command line interface in Golang I chose to use the cli.go package. Go app.Commands = []cli.Command{ { Name: "register", Usage: "register a directory", Action: register, Flags: []cli.Flag{ cli.StringFlag{ Name: "directory, d", Value: "./", Usage...
In this article we show how to parse command-line arguments in Golang with flag package. The package flag implements command-line flag parsing. The command-line arguments are available in the os.Args slice. The flag package allows for more flexible of them. ...
Kingpin - A Go (golang) command line and flag parser Overview Kingpin is afluent-style, type-safe command-line parser. It supports flags, nested commands, and positional arguments. Install it with: $ go get github.com/alecthomas/kingpin/v2 ...
The os.Args holds the command-line arguments. The first value in this slice is the name of the program, while the os.Args[1:] holds the arguments to the program. The individual arguments are accessed with indexing operation. $ go version go version go1.22.2 linux/amd64 ...
Kingpin can also produce complex command-line applications with global flags, subcommands, and per-subcommand flags, like this: $ chat --help usage: chat [<flags>] <command> [<flags>] [<args> ...] A command-line chat application. Flags: --help Show help. --debug Enable debug mode....