luhn

package module
v0.0.0-...-e52d92f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2024 License: MIT Imports: 2 Imported by: 0

README

Luhn Algorithm Package for Go

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.

Installation

To install the package, use go get:

go get github.com/phedde/luhn-algorithm

Usage

Import the package in your Go code:

import "github.com/phedde/luhn-algorithm"
Available Functions

The package provides four main functions:

1. Generate Full Number with Check Digit
number, err := luhn.FullNumber(7992739871)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Full number with check digit: %d\n", number) // prints 79927398713
2. Calculate Check Digit Only
checkDigit, err := luhn.CheckDigit(7992739871)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Check digit: %d\n", checkDigit) // prints 3
3. Validate a Number
isValid := luhn.IsValid(79927398713)
fmt.Printf("Is valid: %v\n", isValid)
Error Handling

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

Example

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
}

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckDigit

func CheckDigit(number int64) (int, error)

CheckDigit calculates the check digit for the given number

func FullNumber

func FullNumber(number int64) (int64, error)

FullNumber calculates the full number with the check digit

func IsValid

func IsValid(number int64) bool

IsValid checks if the number is valid according to the Luhn algorithm

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL