/go-luhn

Create or validate a Lühn (mod 10) check digit in a numeric string in Go.

Primary LanguageGoMIT LicenseMIT

go-luhn

Build Status Go Reference

Create or validate a Lühn (mod 10) check digit in a numeric string in Go.

Examples

Checksum returns the Lühn check digit for number.

number := "7992739871"

checkdigit, _ := luhn.Checksum(number) // Ignoring error for simplicity
fmt.Println("The Lühn check digit for", number, "is", checkdigit)

// Output:
// The Lühn check digit for 7992739871 is 3

Sign returns number with its Lühn check digit appended

number := "7992739871"

number, _ = luhn.Sign(number) // Ignoring error for simplicity
fmt.Println("Your account number is", number)

// Output:
// Your account number is 79927398713

Valid returns whether number verifies against its appended check digit

number := "79927398713"

if luhn.Valid(number) {
    fmt.Println("The number is valid")
} else {
    fmt.Println("The number is not valid")
}

// Output:
// The number is valid