From the course: Learning Go
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Using math operators - Go Tutorial
From the course: Learning Go
Using math operators
- [Instructor] Go supports the same set of mathematical operators that work in other C-style languages. These include the standard operators such as plus, minus, times, and divide, and there's also a remainder operator. You also get all of the Bitwise operators that you'll see in C and Java. There's also a very important rule to understand in Go that's a little bit different than some other languages. In Go, when you do math, all of the values that you operate with have to be of the same type. There is no implicit conversion of one type to another in Go. So for example, you can't add an integer to a float value. Take a look at this code. I have an integer and a float64 and I try to add them together. I can't do that in Go. The result will be an invalid operation error. To fix this, you have to convert the integer to the float64. Each of the built-in types in Go has a matching function that will do the conversion for you. So to change an int to a float64, wrap it inside the float64…