Previously, you learned that you cannot assign a float64 value to an int variable. Fortunately, Go is
not always that rigid. Actually, Go just wants you to be explicit on everything.
That's why it doesn't let you assign a float64 value to an int value. Since it actually wants
your approval.
In the lecture, you're going to learn about the type conversion expression.
Basically, a type conversion creates a new value by changing the type of the original value to another
type.
All right, let's see how it works.
This is a type conversion expression.
It looks like a function call, right?
But, instead of a function name, it starts with a type name.
So first, you type the name of the "type", and within the parenthesis, you provide it a value. This conversion
expression changes the type of a given value to another type. As you know, a variable has a value and
in Go,
there are also function values, constant values, interface values, and the list goes on.
So the conversion expression is not only for converting the type of variables. You can use it to convert
almost any value to another type as well. Of course,
to do that, the type of the value should be convertible to that type that you want to convert into.
Anyway, let's take a look at an example now.
OK.
First, I'm going to show you what you can do.
I'm going to assign a new value to the speed variable.
I'm going to type "speed", and I'm going to multiply it with the force variable.
So in the end, the result of this expression will be assigned to the speed variable. As you now know, these
variables have different types.
So the type of the speed variable is int, whereas the type of the force variable is float64.
So when you run this program, there would be an error like this.
It clearly tells you that the speed variable is an int but the force variable is a float64. As you've
seen before,
you cannot use different types of values together even though they both are numeric types.
I have said enough about the problem.
Let's talk about the solution now.
Let's check it out. To solve the problem,
I need the convert one of these variables.
So, which one of these variables should I convert first?
Should I convert the speed variable into float64, or should I convert the force variable into an int?
For now, let's convert the force variable to an int type like this.
Alright, now let's print out the result, and then let's run it to see what it does. OK.
This time, it works.
It works because inside the expression the force variable's value becomes an int on the fly.
However, this result is not the result that I would like to see. Think about it. Multiplying 100 with
2.5 is 250, right? It's not 200.
So, can you guess why the result is wrong?
Clearly, int(force) conversion trimmed the fractional part of the force variable's value.
So it converts 2.5 to 2.
That's because, an int value can't have a fractional part.
So this is a destructive operation. Since the fractional part literally vaporizes. As you can see,
the conversion can also change the value itself, not only its type.
Anyway, let's also print the force variable, before and after the type conversion.
Let's run it now. As you can see, the force variable is still the same, but the converted value is
different, right?
This means that the conversion creates a new value but it doesn't change the actual force variable.
So the conversion doesn't change the original value.
Now let's talk about how to solve the previous information destruction problem.
So instead of converting the force variable, this time, let's first convert the speed variable.
However, this is not enough.
It's because, the speed variable is not a float64,
it's an int. If I would run this, Go compiler will terminate (end/exits).
Let me show you the same code in the editor.
Let's take a look at the error here. Go (compiler) says that I cannot assign a float64 value to an int variable.
It's because the type of this expression is
float64, not int. Since the type conversion has to change the type of the speed to float64 here.
However, the speed variable here is an int. So I can't assign a float64 value to the speed variable.
To do that,
I also need to convert this whole expression into an int. Let's get back and do that.
Now, let's run it again.
Now, the result is correct.
100 multiplied by 2.5 equals 250, right? As you can see, the order
of conversions and which value to convert first are very important for the correct results.
Let me explain why this works. In the expression, it first converts the value of the speed variable.
So it becomes 100.0 which is float64 value.
Then it multiplies the result with the force variable.
This means it multiplies two float values. So it multiplies 100.0 by 2.5. So the result
becomes 250.0
which is another float value. And, in the end, it converts the result value of 250.0 to an int
value.
So the result becomes 250 which is an int value. Alright.
That's all for now.
In the next lecture, I'm going to show you another example.