Cli
65 articles
Build CLI with cobra
Build a Go CLI with Cobra by installing the CLI generator, initializing a project, adding commands, and building the binary.
CLI with viper config
Build a Go CLI by combining Cobra for command parsing with Viper for flexible configuration management.
Cobra vs urfave/cli: Which CLI Library to Use in Go
Use spf13/cobra for complex CLIs with subcommands and urfave/cli for simpler, lightweight tools.
Fix: "cannot find package" After Installing Go
Fix 'cannot find package' by adding Go's bin directory to your PATH environment variable.
Fix: "go: command not found" on macOS, Linux, and Windows
Fix 'go: command not found' by adding /usr/local/go/bin to your system PATH environment variable.
Go Environment Variables Explained: GOPATH, GOROOT, GOBIN, GOPROXY
Go environment variables like GODEBUG control toolchain behavior and runtime defaults for compatibility and security.
How to Add a Configuration File to a Go CLI App
Define a struct, create a JSON file, and use encoding/json to load settings into your Go CLI app.
How to Add Color Output to a Go CLI
Add color output to a Go CLI by installing the fatih/color library and wrapping print statements with color functions.
How to Add Dependencies with go get
Use `go get` to download and add a package to your module's `go.mod` file, but prefer `go get` only for adding new dependencies or updating specific versions, as `go mod tidy` is the standard for cleaning up unused imports.
How to Add Flags and Arguments to a Go CLI
Define flags using the flag package and access positional arguments via flag.Args() after calling flag.Parse().
How to Add Progress Bars to a Go CLI
Add a visual progress bar to a Go CLI using the schollz/progressbar library to track task completion.
How to Add Shell Completion (Bash, Zsh, Fish) to a Go CLI
Generate and install shell completion scripts for Bash, Zsh, and Fish using the cobra library's completion command.
How to Add Subcommands to a Go CLI with Cobra
Add subcommands to a Cobra CLI by defining a new Command struct and appending it to the parent command's Commands slice.
How to Build a CLI Application in Go
Initialize a Go module, write a main function to handle arguments, and compile the binary to create a CLI application.
How to Build a CLI Task Manager in Go
Build a basic Go CLI task manager using the flag package to handle add and list commands.
How to Build Static Binaries in Go
Build a static Go binary by setting CGO_ENABLED=0 or using specific ldflags to link without external dependencies.
How to Create a REPL in Go
Create a Go REPL by looping through os.Stdin reads and printing results to os.Stdout.
How to Create Interactive Prompts in a Go CLI
Create interactive Go CLI prompts by reading user input with bufio.NewReader and os.Stdin.
How to Cross-Compile Go Programs for Different OS and Architecture
To cross-compile Go programs, simply set the `GOOS` and `GOARCH` environment variables before running `go build`, or pass them as flags directly to the command.
How to Cross-Compile Go Programs (GOOS and GOARCH)
To cross-compile a Go program, simply set the `GOOS` and `GOARCH` environment variables before running `go build`, which tells the compiler to generate binaries for a different operating system and CPU architecture than your host machine.
How to Display Tables in a Go CLI
Use the tablewriter library to format and display tabular data in a Go CLI application.
How to Distribute Go CLI Binaries (GoReleaser, Homebrew)
Distribute Go CLI binaries by automating builds with GoReleaser and publishing them via a Homebrew tap for easy installation.
How to Embed Files in a Go Binary with go:embed
Embed files into a Go binary using the //go:embed directive to include static assets at compile time.
How to Generate Documentation for Your Go Package
Generate Go package documentation locally using the go doc command or publish to pkg.go.dev.
How to Initialize a Go Module with go mod init
Run `go mod init` followed by your module's import path to create a new `go.mod` file, which tracks your project's dependencies and defines the module name.
How to Install Multiple Go Versions Side by Side
Install multiple Go versions side-by-side using the golang.org/dl package to create separate executables for each version.
How to Manage Go Versions with asdf
Install the asdf-go plugin and use asdf install, global, and local commands to manage Go versions.
How to Manage Go Versions with goenv
Use `goenv` to install multiple Go versions and switch between them seamlessly by setting the desired version in your shell configuration or via the `goenv local` command for project-specific isolation.
How to Parse Command-Line Flags in Go
Parse command-line flags in Go using the standard `flag` package to define variables and call `flag.Parse()`.
How to Pass Command-Line Arguments in Go with os.Args
Access command-line arguments in Go by reading the os.Args slice, where index 0 is the program name and index 1+ are user inputs.
How to Read from stdin and Write to stdout in a CLI
Read from stdin and write to stdout in Go using os.Stdin, os.Stdout, and bufio.Scanner for efficient line processing.
How to Remove Unused Dependencies with go mod tidy
Run `go mod tidy` in your module root to automatically remove unused dependencies from `go.mod` and `go.sum`, while adding any missing ones required by your code.
How to Run a Go Program: go run vs go build
Use go run for quick testing and go build to create a permanent executable file for deployment.
How to Set Up GoLand for Go Development
Install Go, initialize a workspace, and configure GoLand to use the gopls language server for immediate development support.
How to Set Up Go on Apple Silicon (M1/M2/M3/M4)
Install Go on Apple Silicon by downloading the darwin-arm64 binary, extracting it to /usr/local/go, and updating your shell PATH.
How to Set Up GOPATH and GOROOT Correctly
For Go versions 1.16 and later, you generally do not need to manually set GOPATH or GOROOT because the Go toolchain uses a default workspace in your home directory and automatically detects the Go installation.
How to Set Up Neovim for Go Development with gopls
Install gopls and configure Neovim settings to enable Go language support and vulnerability scanning.
How to Set Up Vim for Go with vim-go
Install vim-go via git, run the installer to fetch Go tools, and configure your vimrc to enable Go support.
How to Set Up VS Code for Go Development
Install the Go extension and configure gopls in VS Code to enable intelligent Go development features.
How to Uninstall Go Completely
Uninstall Go by deleting the /usr/local/go directory and removing GOROOT and GOPATH from your shell environment variables.
How to Update Dependencies in Go
Use `go get -u` to update all dependencies to their latest compatible versions, or `go get <module>@latest` to target a specific package.
How to Use Build Constraints (//go:build)
Use //go:build directives at the top of Go files to control compilation based on OS, architecture, or custom tags.
How to Use Cobra for Building CLIs in Go
Use the Cobra CLI generator to scaffold a Go project and define commands in main.go to build a functional command-line interface.
How to use flag package
Import flag, define variables, bind them with flag functions, call flag.Parse(), and use the variables in your code.
How to Use flag Package for CLI Arguments in Go
Use the flag package to define variables and call flag.Parse() to read command-line arguments in Go.
How to Use go clean to Remove Build Artifacts
Use `go clean` to remove object files and cached binaries generated during the build process, effectively resetting your module's build state without touching your source code.
How to Use go doc and godoc for Documentation
Use the go doc command to instantly view documentation for Go packages, symbols, and files directly in your terminal.
How to Use go:embed for CLI Templates and Assets
Use `go:embed` to compile static assets like HTML templates, JSON configs, or binary files directly into your Go binary, eliminating the need for external file dependencies or complex bundling steps.
How to Use go env to View and Set Environment Variables
View and set Go environment variables using the go env command to control build paths, proxies, and compiler settings.
How to Use go fmt and gofmt for Code Formatting
Use `go fmt` to automatically format your Go code according to the community standard, as it is the preferred tool that handles package discovery and imports correctly.
How to Use go generate for Code Generation
Run go generate to execute code generation commands defined in //go:generate directives within your source files.
How to Use goimports for Import Management
Use `goimports` to automatically format your Go code and manage imports by adding missing ones, removing unused ones, and grouping them in the standard order.
How to Use go install to Install Binaries
Use `go install` to compile a package and place the resulting binary in your `$GOPATH/bin` (or `$GOBIN`) directory, which is typically added to your system PATH.
How to Use go list to Inspect Packages and Modules
Use `go list` to query package metadata, dependency versions, and build constraints directly from your terminal without compiling code.
How to Use GoReleaser to Release Go Binaries
Automate Go binary releases by adding a GitHub Actions workflow that runs GoReleaser on new tags.
How to Use go run for Quick Execution
Use `go run` to compile and execute Go source files in a single step without creating a binary artifact, making it ideal for quick scripts, testing logic, or prototyping.
How to Use go tool link and go tool compile
Use go tool compile to create object files and go tool link to combine them into an executable binary.
How to Use Homebrew to Distribute Go CLI Tools
Create a custom Homebrew tap with a Ruby formula to build and install your Go CLI tool for distribution.
How to Use the cobra Library for CLI Configuration
Use spf13/cobra to define CLI commands and spf13/viper to handle configuration files and flags for Go applications.
How to Use the Go Playground Effectively
Write self-contained example functions in _test.go files with no parameters to run them in the Go Playground.
How to Use urfave/cli for Building CLIs in Go
Build Go CLIs quickly by defining commands and flags in a cli.App struct and running it with app.Run(os.Args).
How to Write a Go Script That Runs Like a Shell Script
Write a Go program with a main function and run it using go run or compile it to a binary for shell-script-like execution.
What Is GOPATH and Do I Still Need It with Go Modules
GOPATH is a legacy workspace setting that is no longer required for modern Go development using Go Modules.
What Is gopls and How to Configure It
gopls is the official Go language server for editor intelligence, configured via editor settings and verified with the gopls -v version command.
What Is GOROOT and When Should You Change It
GOROOT defines the Go installation path and should only be changed to switch versions or develop the language itself.