upc

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

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

Go to latest
Published: Jun 14, 2022 License: Unlicense Imports: 2 Imported by: 0

README

Golang Library for UPC and EAN

A Go library for parsing, validating and analyzing UPCs and EAN-13/GTIN-13 codes.

  • Validate the accuracy of UPC and EAN/GTIN codes.
  • Parse UPC codes to determine the code type and other details (Manufacturer, Coupon Value, Product Code, etc)

Code Support

  • UPC (Universal Product Code)
  • EAN/GTIN 13 (European Article Numbering or Global Trade Item Number)
  • JAN (Japanese Article Numbering)

The library works with 12 digit UPC (Universal Product Code) codes and 13 digit EAN (European Article Number) codes. EAN is also referred to as GTIN or Global Trade Item Number. The numbers are the same.

Usage

u, err := upc.Parse("045496830434")
if err == upc.ErrInvalidCheckDigit {
    fmt.Println("There's a typo in the UPC")
} else if err != nil {
    fmt.Printf("Something's wrong with the UPC: %s", err)
} else {
    fmt.Printf("Number system: %d\n", u.NumberSystem())
    fmt.Printf("Check digit: %d\n", u.CheckDigit())
    if u.IsGlobalProduct() {
        fmt.Printf("Manufacturer code: %d\n", u.Manufacturer())
        fmt.Printf("Product code: %d\n", u.Product())
    } else if u.IsDrug() {
        fmt.Printf("Drug code: %d\n", u.Ndc())
    } else if u.IsLocal() {
        fmt.Println("UPC intended only for local use")
    } else if u.IsCoupon() {
        fmt.Printf("Manufacturer code: %d\n", u.Manufacturer())
        fmt.Printf("Family code: %d\n", u.Family())
        fmt.Printf("Coupon value: $0.%02d\n", u.Value())
    } else {
        panic("Preceeding categories are exhaustive")
    }
}

Installation

go get -u github.com/vgpc/upc

Documentation

Overview

Package upc provides for parsing, validating and analyzing a UPC (Universal Product Code). For example,

u, err := upc.Parse("045496830434")
if err == upc.ErrInvalidCheckDigit {
	fmt.Println("There's a typo in the UPC")
} else if err != nil {
	fmt.Printf("Something's wrong with the UPC: %s", err)
} else {
	fmt.Printf("Number system: %d\n", u.NumberSystem())
	fmt.Printf("Check digit: %d\n", u.CheckDigit())
	if u.IsGlobalProduct() {
		fmt.Printf("Manufacturer code: %s\n", u.Manufacturer())
		fmt.Printf("Product code: %d\n", u.Product())
	} else if u.IsDrug() {
		fmt.Printf("Drug code: %d\n", u.Ndc())
	} else if u.IsLocal() {
		fmt.Println("UPC intended only for local use")
	} else if u.IsCoupon() {
		fmt.Printf("Manufacturer code: %s\n", u.Manufacturer())
		fmt.Printf("Family code: %d\n", u.Family())
		fmt.Printf("Coupon value: $0.%02d\n", u.Value())
	} else {
		panic("Preceeding categories are exhaustive")
	}
}

Index

Constants

This section is empty.

Variables

View Source
var ErrEanInvalidCheckDigit = errors.New("EAN has an invalid check digit")
View Source
var ErrEanTooLong = errors.New("EAN is too long (must be 13 digits)")
View Source
var ErrEanTooShort = errors.New("EAN is too short (must be 12 digits)")
View Source
var ErrInvalidCheckDigit = errors.New("UPC has an invalid check digit")
View Source
var ErrTooLong = errors.New("UPC is too long (must be 12 digits)")
View Source
var ErrTooShort = errors.New("UPC is too short (must be 12 digits)")

Functions

This section is empty.

Types

type Ean

type Ean int64

Ean represents a European Article Number. To reduce memory consumption, it's stored as a 64-bit integer without the check digit.

func ParseEan

func ParseEan(s string) (Ean, error)

Parse parses a string into a Ean value. The following errors can be returned in addition to integer parsing errors:

ErrEanTooShort
ErrEanTooLong
ErrEanInvalidCheckDigit

func (Ean) CheckDigit

func (e Ean) CheckDigit() int

CheckDigit returns the check digit that should be used as the 13th digit of the EAN.

func (Ean) IsJan

func (e Ean) IsJan() bool

IsJan returns true if the number begins with 45 or 49 JAN (Japanese Article Numbering) codes must begin with one of these two numbers

func (Ean) String

func (e Ean) String() string

String returns the standard, 13-digit string representation of this EAN.

type Upc

type Upc int64

Upc represents a Universal Product Code. To reduce memory consumption, it's stored as a 64-bit integer without the check digit.

func Parse

func Parse(s string) (Upc, error)

Parse parses a string into a Upc value. The following errors can be returned in addition to integer parsing errors:

ErrTooShort
ErrTooLong
ErrInvalidCheckDigit

func (Upc) CheckDigit

func (u Upc) CheckDigit() int

CheckDigit returns the check digit that should be used as the 12th digit of the UPC.

func (Upc) Family

func (u Upc) Family() int

Family returns the coupon manufacturer's family code. It designates the kind of products to which the coupon applies.

func (Upc) IsCoupon

func (u Upc) IsCoupon() bool

IsCoupon returns true if the number system is 5. These UPCs are intended for labeling coupons. See Family and Value methods.

func (Upc) IsDrug

func (u Upc) IsDrug() bool

IsDrug returns true if the number system is 3. These UPCs are intended for labeling drugs by their National Drug Council number. See Ndc method.

func (Upc) IsGlobalProduct

func (u Upc) IsGlobalProduct() bool

IsGlobalProduct returns true if the number system is 0, 1, 6, 7, 8 or 9. These UPCs are intended for global use with products (in contrast to local use or coupon use, etc.)

func (Upc) IsLocal

func (u Upc) IsLocal() bool

IsLocal returns true if the number system is 2 or 4. These UPCs are intended for local/warehouse use.

func (Upc) Manufacturer

func (u Upc) Manufacturer() string

Manufacturer returns the 6-digit manufacturer code assigned by a GS1 organization. The return value is a string because leading zeros are used for lookups in the standard GS1 databases. In the case of coupons, the manufacturer is only 5 digits long.

func (Upc) Ndc

func (u Upc) Ndc() string

Ndc returns the National Drug Code associated with a UPC. The value is only meaningful if IsDrug returns true for the UPC.

The value is a string because leading zeros are meaningful in the FDA database of labeler codes. No attempt is made to put the NDC code into standard format with dashes.

func (Upc) NumberSystem

func (u Upc) NumberSystem() int

NumberSystem returns the first digit of the UPC, also known as the "number system".

func (Upc) Product

func (u Upc) Product() int

Product returns the product code assigned by a manufacturer.

func (Upc) String

func (u Upc) String() string

String returns the standard, 12-digit string representation of this UPC.

func (Upc) Value

func (u Upc) Value() int

Value returns the coupon value in pennies. This is the amount saved by applying the coupon. The value is always less than 100.

Jump to

Keyboard shortcuts

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