luhnmod10

package module
v0.0.0-...-4517d2c Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2022 License: MIT Imports: 0 Imported by: 0

README

Luhn (Mod 10)

Go docs

A fast and simple in-place implementation of the luhn check algorithm in Go. Implementations in other languages can be found at github.com/luhnmod10.

Usage

go get github.com/luhnmod10/go
_ = luhnmod10.Valid("4242424242424242")

Contributing

Contributions are welcome! If you can improve the execution time of this implementation without increasing its complexity, please open a pull request. To test your change, run make in the repository to run the tests and the benchmarks.

Documentation

Overview

Package luhnmod10 is a fast and simple in-place implementation of the luhn check algorithm.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckDigit

func CheckDigit(number string) int

CheckDigit returns the check digit of a number string that can be used as the final digit to make a number string pass the luhnmod10 validity check. Use Valid to check if a number string is valid. The number passed to the function must contain only numeric characters otherwise behavior is undefined.

Example
number := "424242424242424"
checkDigit := luhnmod10.CheckDigit(number)
fmt.Println(number, "check digit:", checkDigit)
Output:

424242424242424 check digit: 2

func Checksum

func Checksum(number string) int

Checksum returns the checksum of a number string that can be used to determine if the number satisfies the luhnmod10 validity check. Use Valid to check if a number string is valid. The number passed to the function must contain only numeric characters otherwise behavior is undefined.

Example
number := "4242424242424241"
checksum := luhnmod10.Checksum(number)
fmt.Println(number, "checksum:", checksum)
Output:

4242424242424241 checksum: 79

func Valid

func Valid(number string) bool

Valid returns true if the number string is luhn valid, and false otherwise. The number passed to the function must contain only numeric characters otherwise behavior is undefined.

Example (False)
number := "4242424242424241"
valid := luhnmod10.Valid(number)
fmt.Println(number, "is valid:", valid)
Output:

4242424242424241 is valid: false
Example (True)
number := "4242424242424242"
valid := luhnmod10.Valid(number)
fmt.Println(number, "is valid:", valid)
Output:

4242424242424242 is valid: true

Types

This section is empty.

Jump to

Keyboard shortcuts

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