Welcome back again, let's continue.
Alright. Now I'm going to show you how Printf provides type safety using verbs. I'm going to change
the verbs here.
Let's change "%v" to "s", for example. As you know, the planet variable is a string.
So I have used "%s" here. "%s" means string.
It should only be used with string values.
Now let's also change "%v" here to %d.
Because distance is an integer.
"%d" means decimal.
Now, let's change "%v" here to "%f". It's because the orbital variable is a float.
So, "%f" means float.
The last two. Let's change %v here to %s.
And lastly, let's change %v here to %t.
This is because the hasLife variable is a bool.
%t verb prints true or false.
%t verb prints true or false
depending on the value of the variable.
Now all the printfs here are type-safe.
I'll show you what I mean in a minute.
Actually, when you use the %v verb, printf replaces %v verb with a typed verb under
the hood.
So printf is doing what I've done here behind the scenes.
You might ask: "Why should I use these new verbs rather than just using the swiss-knife %v verb?"
Right.
It's a good question.
However, you need to wait a little bit to get an answer for this.
After this example, I'll explain it. No worries.
First, run this one. As you can see, the result is almost the same as before, only the orbital here has more
precision than the previous one.
Precision means how many decimals there are.
Let me show you what I mean by the type-safety.
I'm going to change this %s here to
%d. As you know, %d should only be used with an integer value.
Right.
However, here planet (variable) is a string.
Let's see what happens. As you can see, Go warns me with a yellow underline.
Go says that planet is a string.
But you used a %d verb.
%d verb is only for integers you smart one.
OK let's change the others. Now it says that distance is an int but you used a verb for float values.
And here it says that orbital is a float but you used a verb for string values.
And here again it says that the planet is a string.
But you used a verb for integer values.
And, lastly, here it says that life is a boolean.
But you used a verb for integer values.
OK let's quickly fix those now.
So this is what I mean by the type safety. Go compares the type of a value and its corresponding verb.
If their type doesn't match then it warns you. Sometimes, type checking prevents unintentional errors.
So always try to use the typesafe verbs instead of the swiss-knife %v verb.
All right.
This is my recommendation.
All right.
Now let's take a look at the last example here.
I'm going to show you how to change the printed precision of a float number.
You'll see what I mean in a second.
OK let's first print the orbital as usual using the f verb.
Now it prints as the previous one, right.
Let's get back. Let's add a new line here to make it more readable in the console.
OK let me copy this one here.
Now I'm going to change the precision of this verb to zero.
It prints 225 days. Much more readable,
right.
But it has lost its precision.
This is what precision means. Instead of printing 224.701,
as like here, it prints 225 days as in here.
OK let's get back. Let's print it with one precision this time.
Let's try. The difference is clear. Whenever you increase the precision, it becomes much more precise.
And at the same time, it becomes much more unreadable.
Let me add a bunch of these with increasing precision.
All right.
Let's try it. As you can see, the precision increases on each line.
This means that some verbs have special properties like the f verb. In this course,
you're going to learn about them as you progress.
No worries.
All right.
Good job.
Please work on the exercises if you want to really learn about printf.
Thank you for watching so far.
See in the next lecture. Bye!