From the course: Go Essentials: Concurrency, Connectivity, and High-Performance Apps
Numbers and assignments - Go Tutorial
From the course: Go Essentials: Concurrency, Connectivity, and High-Performance Apps
Numbers and assignments
- [Instructor] Let's calculate the mean or average of two numbers. So we start with package main, and we import "fmt" to show the results. And now we define our function, func main. We define a variable x, which is an integer, and y, which is an integer. In Go, we have several integer types. We have int and uint for signed integer and unsigned integer, and these are the integers native to the current machine. We also have integers in specific size. We have the signed ones with 8, 16, 32, or 64-bits and the same versions, but unsigned. Now we assign values to these integers, x = 1 and y = 2, and now we print. I'm going to use Printf with %v, which is going to print any goal value, and %T, which is going to print the type. So I'm going to print the type of x, type of y, and then calculate the mean, x + y divided by 2, and print out the result. Now I can do Run, Run Without Debugging. And if you don't see the results, View and Debug Console. And we see that x of type int and y of type int, and the result is 1 of type int. When we divide two integers, we are going to get an integer back. So let's change this a bit. I'm going to comment out this part, and I'm going to fold this comment, and then I'm going to uncomment the other parts. And this time I'm going to make x and y float64s. In Go, we have only two kinds of floats, a 32-bit floating number and a 64-bit floating number. Let's run it again. So Run, Run Without Debugging, and this time we see that the type of the variables is float64, and the result is 1.5, which is expected. So this is the long variable declaration, but we are going to do something else, so let me comment this one as well. And I'm going to open the comment below, and let me fold these comments on the top. So now we can do x := and not just equal. And := is for defining and assigning value to a variable at the same time. And Go is doing type inference, meaning Go will look on the value on the right side of the :=, the first type, and then set the variable type to the one we want. And if you're going to run this one, we will see exactly the same result. And, finally, we can go even a little bit more shorter. We can assign to two values at the same time. Let me fold this comment. So I can do in the same line x and y :=, and then put two values on the right side. And if I'm running this code, I'm going to see the same result. If I try to make one variable an integer and the second one a float, this is not going to work. The Go type system is very strict and will not allow you to do mathematical operations between variables in a different type. Another thing is that if you're going to declare a variable, but you're not going to use it, let's say I'm going to do z := 3, Go is going to make this a compilation error. If I try to run this without debugging, you will see that it will compile. It will tell me the variable is declared but not used. So you cannot have unused variables in Go.
Contents
-
-
-
-
Numbers and assignments3m 52s
-
(Locked)
Conditionals2m 35s
-
(Locked)
For loops2m 5s
-
(Locked)
Challenge: FizzBuzz57s
-
(Locked)
Solution: FizzBuzz55s
-
(Locked)
Strings2m 59s
-
(Locked)
Challenge: Even-ended numbers55s
-
(Locked)
Solution: Even-ended numbers53s
-
(Locked)
Slices2m 34s
-
(Locked)
Challenge: Find the maximal value12s
-
(Locked)
Solution: Find the maximal value41s
-
(Locked)
Maps2m 37s
-
(Locked)
Challenge: Maps26s
-
(Locked)
Solution: Maps57s
-
-
-
-
-
-
-
-