THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
In this blog post, Learn the below things in the go language.
In this example.
multiply operator
and the result is assigned to an integer variable.%d
in the Printf function.package main
import (
"fmt"
)
func main() {
var number1, number2 int
number1 = 5
number2 = 6
result: = number1 * number2
fmt.Printf("Multiply result is %d\n", result)
}
Output:
Multiply the result is 30
In this program.
multiply operator
and the result is assigned to a third float variable.%f
for float in the Printf
function.package main
import (
"fmt"
)
func main() {
var number1, number2 float64
number1 = 5.1
number2 = 6.3
result: = number1 * number2
fmt.Printf("Multiply floating numbers result is %f\n", result)
}
Output:
Multiply floating numbers result is 32.130000
When you are multiplying with different types (int
and float
), you got the error invalid operation: number1 * number2 (mismatched types int and float64).
The following program gives an error.
package main
import (
"fmt"
)
func main() {
var number1 int
var number2 float64
number1 = 5
number2 = 6.3
result: = number1 * number2
fmt.Printf("Multiply float and int numbers result is %f\n", result)
}
Output:
# command-line-arguments
Test.go:12:20: invalid operation: number1 * number2 (mismatched types int and float64)
Importantly,
The number must be float when multiplied with the float number.
So, the int
type needs to convert to float
using float64
(intvalue)
Here is a working code
package main
import (
"fmt"
)
func main() {
var number1 int
var number2 float64
number1 = 5
number2 = 6.3
result: = float64(number1) * number2
fmt.Printf("Multiply float and int numbers result is %f\n", result)
}
Output:
Multiply float and int numbers result is 31.500000
In this tutorial, Learned the multiplication of numbers and integers in golang
🧮 Tags
Recent posts
Puppeteer Login Test example How to convert Double to Integer or Integer to double in Dart| Flutter By Example Best ways to fix 504 gateway time out in nodejs Applications Fix for Error No configuration provided for scss Multiple ways to List containers in a Docker with examplesRelated posts