Welcome to this coding lecture! In this lecture, I'm going to show you a few examples about Printf.
OK and let's get started.
Now I'm going to show you how you can print the types of variables using Printf.
By the way, you're not limited to variables only. Printf can also print the type of any value whether it's
a variable or not.
First, let me show you an example here. Then, I'm going to open my code editor for further examples.
OK, let's declare a few variables with different types: var speed int
var heat float64, var off bool, var brand as a string variable.
Let's print the type of the speed variable using printf like this.
So to print the type of a value, I'm using "%T" verb here. It means: "type".
Let's run it now. As you can see, it prints int.
Let's take a look at the declaration of the speed variable above.
You can see that Go prints its type as int which is correct.
Right.
Now let's print the type of the heat variable using printf again like this.
Let's see how it prints to the console. It says float64.
That's right.
Now let's print the type of the off variable. It prints bool this time.
Let's print the type of the brand variable. Pause the video and think about what it would print.
Here is the answer.
It prints string as expected.
Alright.
That's all for this example.
Now let's take a look at more examples in the coding editor.
Now I'm going to create a few variables using a multiple declaration statement.
First, I'm going to declare a new variable named planet.
with a string value like this.
Let's print it using printf.
I'm going to type planet. As you can see, you can type anything inside the formatting text.
Now I'm going to type a new verb that you didn't see before. This "%v" verb is like a swiss-army knife
for Printf.
It can print any value.
If you need to remember one thing about Printf, then this is it. OK, now I'm going to pass the planet variable
to Printf.
OK I'm going to run this program.
As you can see, Printf prints the formatter by replacing the v verb with the planet variable ('s value).
This is what I mean by saying that the formatter is like a template. Also notice that it didn't
print a new line.
It's because I didn't tell it to do so.
Let me add a new line escape sequence to the formatter text.
OK, I'm just going to add \n inside here.
Alright, let's try it again.
Did you notice it? Compare the last line with the previous one. This time in the last line, it prints
a new line.
Right.
Now let me declare a few more variables.
Here's the distance variable.
It will store the distance to the planet Venus.
On average Venus is
261 millions of kilometers away from us.
So let's initialize it.
261
Now I'm going to declare another variable named orbital, like this. Venus orbits around the sun about
225 days.
Now, I'm going to declare the last variable: hasLife. As far as we know there is no life on Venus, right?
So I will just type false. OK let me print those variables using printf.
I'm going to copy this line here, three times, using cmd+shift+d (ctrl+shift+d on Windows). Now I'm going to change this planet
words here by using command+d again.
Alright.
Now I'm going to type distance here, and I'm going to pass the distance variable to printf here.
Let me do the same thing for the orbital variable here.
OK.
Alright.
Now I'm going to use two verbs.
I'm going to print,
"does Venus have life?"
False.
First I'm going to type: does.
Then I'll put a verb here.
So I'm going to pass the planet variable able to printf.
This was for the first verb. It'll be replaced with Venus because the planet variable's value is
Venus. And, lastly, I'm going to pass the hasLife variable to printf for the second verb, here.
This verb will be replaced with false because the hasLife variable's value is false.
As I said previously, you can add as many verbs and values to printf as you want.
The rule is that there should be only one formatting text and the number of verbs and the number of
values should match.
OK let me fix this line a little bit.
As long as you end it with a comma you can type it like this.
OK let's try this now. Planet Venus
Distance is 261 Orbital is 224
All right everything looks right.
Let's get back. As I said, the number of verbs and the number of values should match.
Actually, there is an exception to that rule.
It's called argument indexing. By using the argument indexing,
you can refer to the passed values by their position. Let me show you. I'm just going to type Printf.
%v is %v away with a newline character.
Ok now I'm going to pass two variables: planet and distance.
All right so far so good.
This is what you've seen so far.
OK let's run it
It prints an expected message, of course.
All right. Now, without changing the arguments, I'm going to change the formatting text like this.
This is an argument index, and v here is just the usual v verb
as you know. So it's typed like this.
A percent sign and an index, then a formatting verb.
So two hear tell Printf: "Get me the second argument". And, Printf replaces this whole verb with the
distance variable.
This is because if you exclude the formatting text, the distance here is the second argument to the printf.
So it will replace this verb with the distance.
And similarly, printf will also replace %[1]v here using the planet variable.
This is because the planet variable's index position is one.
It is the first argument to the Printf.
So in Printf, the indexes start at 1.
All right.
Let's run this, as you can see,
now the printf prints the values multiple times.
Even I've passed two variables.
OK, just bookmark this.
Then you can return back to it later whenever you want.
OK I'm going to finishing here. So I'm going to continue in the next lecture.
OK see you, bye bye.