You build a CLI application in Go by initializing a module, writing a main function that parses arguments, and compiling the binary with go build.
package main
import (
"fmt"
"os"
)
func main() {
if len(os.Args) > 1 {
fmt.Println("Hello,", os.Args[1])
} else {
fmt.Println("Hello, World")
}
}
- Initialize a new module with
go mod init myapp. - Create a file named
main.goand paste the code above into it. - Build the executable binary using
go build -o myapp. - Run your new CLI tool by executing
./myapp.