go run hello.go uses run and hello.go as arguments to the go program.
Accessing Arguments with os.Args
Go provides access to raw command-line arguments through theos.Args slice. The first value in this slice is always the path to the program itself, while os.Args[1:] contains the actual arguments passed to the program.
Running the Program
To experiment with command-line arguments, it’s best to build a binary withgo build first:
The first element of
os.Args is always the program name. Use os.Args[1:] to access only the user-provided arguments.Key Points
os.Args Structure
The first element (
os.Args[0]) is the program path, followed by the actual arguments.Array Indexing
Access individual arguments using standard slice indexing like
os.Args[3].