diff options
Diffstat (limited to 'barcode/barcode.go')
-rw-r--r-- | barcode/barcode.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/barcode/barcode.go b/barcode/barcode.go new file mode 100644 index 0000000..f400002 --- /dev/null +++ b/barcode/barcode.go @@ -0,0 +1,35 @@ +package barcode + +import ( + "strconv" + "strings" + + "github.com/neonxp/checksum" +) + +// Check barcode +func Check(number string) error { + sum := 0 + numbers := strings.Split(number, "") + l := len(numbers) + for i := l; i > 0; i-- { + ch := numbers[i-1] + num, err := strconv.Atoi(string(ch)) + if err != nil { + return checksum.ErrInvalidNumber + } + if (l-i)%2 != 0 { + sum += num * 3 + } else { + sum += num + } + } + if sum%10 != 0 { + return checksum.ErrInvalidChecksum + } + return nil +} + +func Generate(number string) (string, error) { + return "", checksum.ErrNotImplemented +} |