In this section, we're going to see how to process command-line arguments. A typical example of a command-line argument is ls -a
. Here, a
is a command-line argument that is passed to our last command, and ls
is a program located in the operating system. Based on the arguments that are passed to the ls
command, it behaves differently.
For instance, if we type ls
, it will show us all the visible files. If we type ls -a
, then it shows everything under that directory, including invisible items as well, which can be seen in the following screenshot:
So, we're going to do the same thing to our program. You can use os.Args
to read your arguments that are passed to your application. We are going to read and write these arguments to the console and see how it looks after we passed some arguments to our application. We will have to clear our terminal first and type go run main.go
. Since, initially, we are not going to pass any argument, we can expect to see just one...