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 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 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 doing multiply with different types (int
and float
), you got 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 need 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, Learne the multiplication of numbers and integers in golang
🧮 Tags
Recent posts
Naming style camel,snake,kebab,pascal cases tutorial example javascript Add padding, leading zero to a string number How to Display JavaScript object with examples How to convert decimal to/from a hexadecimal number in javascript How to convert character to/from keycode in javascript examplesRelated posts