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 ¶
- Variables
- type Ean
- type Upc
- func (u Upc) CheckDigit() int
- func (u Upc) Family() int
- func (u Upc) IsCoupon() bool
- func (u Upc) IsDrug() bool
- func (u Upc) IsGlobalProduct() bool
- func (u Upc) IsLocal() bool
- func (u Upc) Manufacturer() string
- func (u Upc) Ndc() string
- func (u Upc) NumberSystem() int
- func (u Upc) Product() int
- func (u Upc) String() string
- func (u Upc) Value() int
Constants ¶
This section is empty.
Variables ¶
var ErrEanInvalidCheckDigit = errors.New("EAN has an invalid check digit")
var ErrEanTooLong = errors.New("EAN is too long (must be 13 digits)")
var ErrEanTooShort = errors.New("EAN is too short (must be 12 digits)")
var ErrInvalidCheckDigit = errors.New("UPC has an invalid check digit")
var ErrTooLong = errors.New("UPC is too long (must be 12 digits)")
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 ¶
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 ¶
CheckDigit returns the check digit that should be used as the 13th digit of the 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 ¶
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 ¶
CheckDigit returns the check digit that should be used as the 12th digit of the UPC.
func (Upc) Family ¶
Family returns the coupon manufacturer's family code. It designates the kind of products to which the coupon applies.
func (Upc) IsCoupon ¶
IsCoupon returns true if the number system is 5. These UPCs are intended for labeling coupons. See Family and Value methods.
func (Upc) IsDrug ¶
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 ¶
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 ¶
IsLocal returns true if the number system is 2 or 4. These UPCs are intended for local/warehouse use.
func (Upc) Manufacturer ¶
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 ¶
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 ¶
NumberSystem returns the first digit of the UPC, also known as the "number system".