Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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 (
	"fmt"
	"os"
)

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)
}

Explanation:

  1. os.Args:

    • os.Args is a slice of strings that represents the command-line arguments. The first element (os.Args[0]) is the program name.

  2. argsWithProg:

    • argsWithProg contains all the command-line arguments, including the program name.

  3. argsWithoutProg:

    • argsWithoutProg contains only the command-line arguments (excluding the program name).

  4. arg:

    • arg is an example of accessing a specific command-line argument by index (in this case, the third argument at index 3).

  5. Printing:

    • 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.

PreviousTesting and BenchmarkingNextCommand-Line Flags

Last updated 1 year ago

Was this helpful?