From the course: Learning Go

Unlock this course with a free trial

Join today to access over 24,200 courses taught by industry experts.

Use math operators

Use math operators - Go Tutorial

From the course: Learning Go

Use math operators

- [Instructor] Go supports the same set of mathematical operators that work in other C-style languages, including the usual math operators for addition, subtraction, multiplication, and division, and you also get all of the Bitwise operators that are available in C and Java. As I've mentioned previously, Go doesn't implicitly convert numeric types. So you can't take an integer value and, say, add it to a floating point value without conversion. In this code, I have one variable that's an integer and another one that's a float, and I try to add them together. This will result in an error, crashing the application, and I'll see the error message, invalid operation. To correctly add these values together, I have to convert one of the values to match the type of the other one. And you do this by wrapping the value in a function that has the same name as the target type. So now once again, I'm starting off with an integer…

Contents