Welcome back! In the previous lecture,
you have learned about the Args variable In this lecture, I'm going a code in the coding editor.
Let's get started.
Now first let me show you what the os.Args variable contains.
I'm going to call Printf with os.Args. This will print the values inside the Args slice.
By the way, I'll explain how the Printf function works later on.
Let's run it now.
Here, it prints the Args slice with one string value inside. As you can see, Printf prints the type
of the slice here. and its values here.
If I were using the Println function, then it would only print the values. However, with Printf,
I can also get the type of a variable or value.
Anyway. As I told you, this one is the first parameter of the Args slice.
So this is the path to the running program. But it's a temporary path. So it doesn't exist anymore.
It's because the "go run" tool creates temporary executable files.
However, "go build" (command) creates an executable file permanently.
So to make the program name more meaningful, I'm going to use the "go build" tool instead.
Let's build this program using the go build tool this time. This argument -o
allows me to give a name to the compiled program.
I'm going to name it greeter. As you can see, the greeter program is here.
"go build" has compiled my program, and created an executable program named greeter.
Let's run it now.
I think this looks better. As you can see,
now the first value of the Args slice is greeter (the name of the program).
Let me run it with a few example arguments.
Here's the first argument, and the second one, and the last one. As you can see, Go has loaded up all the
arguments into the Args slice automatically. The first value is still the path to the executing program.
And, the rest are the arguments that I've passed to the program.
And I can get each one of them. Let's do it.
Now, I'm going to the first argument of the Args slice.
and the second one.
and it the third one.
and the last one.
Now, I'm going to show you the len function.
This len function here can find how many items inside a slice value.
Let's build it one more time, and let's run it now. Here it prints each item one by one.
These are the exact same values inside of the Args strings slice, right?
os.Args with an index value of zero here gets the path.
And the index value of one here gets the first argument.
Two gets the second argument. And, three gets the third argument. Cool.
As you can see, it prints four since there are really four items inside the slice. However, note that there are
only three arguments, not four. But the path value as its first value,
the slice's length becomes four.
Alright.
That's all for now. In the next lecture, I'm going to create the actual greeter program finally. See you there.