checkdigit

package module
v0.0.0-...-1851fe9 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2025 License: MIT Imports: 1 Imported by: 0

README

checkdigit

Source

エキスパートたちのGo言語 一流のコードから応用力を学ぶ
1.5 複数のアルゴリズムに対応したチェックディジットライブラリ

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidArgument = errors.New("checkdigit: invalid argument")

ErrInvalidArgument is happening when given the wrong argument.

Functions

This section is empty.

Types

type Generator

type Generator interface {
	Generate(seed string) (int, error)
}

A Generator generates a check digit by implemented algorithm or calculator.

type Provider

type Provider interface {
	Verifier
	Generator
}

func NewJAN13

func NewJAN13() Provider

NewJAN13 returns a new Provider that implemented GTIN-13 with position correction calculator.

func NewLuhn

func NewLuhn() Provider
Example
package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/178inaba/tech-playground/checkdigit"
)

func main() {
	p := checkdigit.NewLuhn()

	const seed = "411111111111111"
	cd, err := p.Generate(seed)
	if err != nil {
		log.Fatalln("failed to generate check digit")
	}

	ok := p.Verify(seed + strconv.Itoa(cd))
	fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

}
Output:

seed: 411111111111111, check digit: 1, verify: true

type Verifier

type Verifier interface {
	Verify(code string) bool
}

A Verifier is verifying to code by implemented algorithm or calculator.

Jump to

Keyboard shortcuts

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