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:
Explanation:
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.
argsWithProg
:argsWithProg
contains all the command-line arguments, including the program name.
argsWithoutProg
:argsWithoutProg
contains only the command-line arguments (excluding the program name).
arg
:arg
is an example of accessing a specific command-line argument by index (in this case, the third argument at index 3).
Printing:
The program prints the command-line arguments to the console for demonstration.
When you run this program from the command line, for example:
The output will be:
Keep in mind that indexing starts at 0, so os.Args[0]
is the program name.
Last updated
Was this helpful?