An open API service indexing awesome lists of open source software.

https://github.com/ferdypruis/go-luhn

Create or validate a Lühn (mod 10) check digit in a numeric string in Go.
https://github.com/ferdypruis/go-luhn

checksum go golang luhn mod10

Last synced: 5 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# go-luhn
[![Build Status](https://travis-ci.com/ferdypruis/go-luhn.svg?branch=master)](https://travis-ci.com/ferdypruis/go-luhn)
[![Go Reference](https://pkg.go.dev/badge/github.com/ferdypruis/go-luhn.svg)](https://pkg.go.dev/github.com/ferdypruis/go-luhn)

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.
```go
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
```go
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
```go
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
```