Command-Line Flags in Go
Go: Command-Line Flags
Go provides a simple way to parse command-line flags using the flag
package.
How to define and parse command-line flags in Go
Here's an example of how to define and parse command-line flags in Go:
package main
import (
"flag"
"fmt"
)
func main() {
// Define a flag "name" with a default value "World" and a description
name := flag.String("name", "World", "the name to greet")
// Parse the command-line arguments
flag.Parse()
// Print the personalized greeting
fmt.Printf("Hello, %s!\n", *name)
}
This code defines a flag -name
with a default value World
and a description using the flag.String
function. It then calls flag.Parse
to parse the command-line arguments and set the value of the name variable. Finally, it prints a personalized greeting using the value of the name variable.
You can define other types of flags using functions like flag.Bool
, flag.Int
, and flag.Float64
, depending on the type of data you want to parse.
Parse a boolean flag in Go
Here's an example of how to define and parse a boolean flag:
package main
import (
"flag"
"fmt"
)
func main() {
// Define a boolean flag "loud" with a default value false and a description
loud := flag.Bool("loud", false, "whether to greet loudly")
// Parse the command-line arguments
flag.Parse()
// Print the personalized greeting, optionally loud
if *loud {
fmt.Printf("HELLO, %s!\n", *name)
} else {
fmt.Printf("Hello, %s!\n", *name)
}
}
This code defines a boolean flag -loud
with a default value false and a description using the flag.Bool
function. It then calls flag.Parse
to parse the command-line arguments and set the value of the loud variable. Finally, it prints a personalized greeting either normally or loudly depending on the value of the loud flag.
I hope that it helps you get started with working with command-line flags in Go! Let me know if you have any more questions.
Previous Article
Next Article
Golang Tutorials
- Hello World
- Operators in Go
- Declarations in Go
- Values in Go
- Variables in Go
- For in Go
- If/Else in Go
- Switch in Go
- Arrays in Go
- Slices in Go
- Maps in Go
- Range in Go
- Functions in Go
- Closures in Go
- Recursion in Go
- Pointers in Go
- Strings and Runes in Go
- Structs in Go
- Methods in Go
- Interfaces in Go
- Generics in Go
- Errors in Go
- Goroutines in Go
- Channels in Go
- Select in Go
- Timeouts in Go
- Timers in Go
- Worker Pools in Go
- WaitGroups in Go
- Mutexes in Go
- Sorting in Go
- Panic in Go
- Defer in Go
- Recover in Go
- JSON in Go
- XML in Go
- Time in Go
- Epoch in Go
- Time Formatting in Go
- Random Numbers in Go
- Number Parsing in Go
- URL Parsing in Go
- SHA256 Hashes in Go
- Base64 Encoding in Go
- Reading Files in Go
- Writing Files in Go
- File Paths in Go
- Directories in Go
- Testing and Benchmarking in Go
- Command-Line Arguments in Go
- Command-Line Flags in Go
- Command-Line Subcommands in Go
- Environment Variables in Go
- HTTP Client in Go
- HTTP Server in Go
- Context in Go
- Signals in Go