Code with Go
Ask or search…

Command-Line Arguments

This Go program demonstrates how to work with command-line arguments using the os.Args variable. Let's go through the code with inline comments:
package main
import (
func main() {
// os.Args provides a slice of command-line arguments, including the program name.
argsWithProg := os.Args
// os.Args[1:] gives a slice of command-line arguments excluding the program name.
argsWithoutProg := os.Args[1:]
// Accessing a specific command-line argument by index.
arg := os.Args[3]
// Print the command-line arguments.
fmt.Println("Args with program name:", argsWithProg)
fmt.Println("Args without program name:", argsWithoutProg)
fmt.Println("Third argument (index 3):", arg)
  1. 1.
    • os.Args is a slice of strings that represents the command-line arguments. The first element (os.Args[0]) is the program name.
  2. 2.
    • argsWithProg contains all the command-line arguments, including the program name.
  3. 3.
    • argsWithoutProg contains only the command-line arguments (excluding the program name).
  4. 4.
    • arg is an example of accessing a specific command-line argument by index (in this case, the third argument at index 3).
  5. 5.
    • The program prints the command-line arguments to the console for demonstration.
When you run this program from the command line, for example:
go run main.go arg1 arg2 arg3
The output will be:
Args with program name: [./main arg1 arg2 arg3]
Args without program name: [arg1 arg2 arg3]
Third argument (index 3): arg3
Keep in mind that indexing starts at 0, so os.Args[0] is the program name.