To calculate the greatest common divisor (GCD) of two integers in Go, you can use the math package's GCD
function. Here's an example:
package main
import (
"fmt"
"math"
)
func main() {
a := 24
b := 36
gcd := math.GCD(int64(a), int64(b))
fmt.Println("GCD:", gcd)
}
In this example, we import the math
package and then use the GCD
function to calculate the GCD of two integers - a
and b
. The GCD
function takes two int64 arguments and returns their greatest common divisor as an int64 value. We then print the result using the fmt.Println
function.