find the greatest common divisor of two numbers in go

Here's one way to find the greatest common divisor of two numbers using the Euclidean algorithm in Go:

main.go
package main

import "fmt"

func gcd(a, b int) int {
    if b == 0 {
        return a
    }
    return gcd(b, a%b)
}

func main() {
    a, b := 42, 56
    fmt.Println(gcd(a, b)) // Output: 14
}
194 chars
16 lines

In this implementation, we declare a function gcd that takes two integers a and b, and recursively computes their greatest common divisor using the mod operator (%).

We initialize the values of a and b in the main function, and then call gcd(a, b) to compute their greatest common divisor. In this case, the output will be 14.

To use this implementation for other pairs of numbers, simply change the values of a and b in the main function.

gistlibby LogSnag