A Go package that implements the Luhn algorithm, also known as the "modulus 10" or "mod 10" algorithm. This package provides functionality to generate and validate numbers that follow the Luhn algorithm, commonly used for validating various identification numbers such as credit card numbers, Social Security Numbers, etc.
To install the package, use go get
:
go get github.com/phedde/luhn-algorithm
Import the package in your Go code:
import "github.com/phedde/luhn-algorithm"
The package provides four main functions:
number, err := luhn.FullNumber(7992739871)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Full number with check digit: %d\n", number) // prints 79927398713
checkDigit, err := luhn.CheckDigit(7992739871)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Check digit: %d\n", checkDigit) // prints 3
isValid := luhn.IsValid(79927398713)
fmt.Printf("Is valid: %v\n", isValid)
All functions that can return errors should be properly handled. The package will return errors in the following cases:
- Input number is not positive
- Invalid digits in the input
- Parsing errors
Here's a complete example showing how to use the package:
package main
import (
"fmt"
"log"
"github.com/phedde/luhn-algorithm"
)
func main() {
// Generate a full number with check digit
number, err := luhn.FullNumber(7992739871)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Full number: %d\n", number) // prints 79927398713
// Calculate just the check digit
checkDigit, err := luhn.CheckDigit(7992739871)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Check digit: %d\n", checkDigit)
// Validate a number
isValid := luhn.IsValid(79927398713)
fmt.Printf("Is valid: %v\n", isValid) // prints true
}
MIT
Contributions are welcome! Please feel free to submit a Pull Request.