eansearch

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: MIT Imports: 10 Imported by: 0

README

A Go module for EAN, GTIN and ISBN name lookup and validation using the API on https://www.ean-search.org

package main

import (
	eansearch "github.com/eansearch/go-ean-search"
	"fmt"
	"os"
)

func printProduct(p eansearch.Product) {
	fmt.Println("EAN:\t", p.Ean)
	fmt.Println("\t Name:", p.Name)
	fmt.Println("\t CategoryID:", p.CategoryID)
	fmt.Println("\t CategoryName:", p.CategoryName)
	fmt.Println("\t IssuingCountry:", p.IssuingCountry)
}

func printExtProduct(p eansearch.ExtProduct) {
	fmt.Println("EAN:\t", p.Ean)
	fmt.Println("\t Name:", p.Name)
	fmt.Println("\t CategoryID:", p.CategoryID)
	fmt.Println("\t CategoryName:", p.CategoryName)
	fmt.Println("\t GoogleCategoryID:", p.GoogleCategoryID)
	fmt.Println("\t IssuingCountry:", p.IssuingCountry)
}

func main() {
	var extproducts []eansearch.ExtProduct
	var products []eansearch.Product
	var more bool
	var err error
	var country string

	// get an API token at https://www.ean-search.org/ean-database-api.html
	token := os.Getenv("EAN_SEARCH_API_TOKEN");
	eansearch.SetToken(token)

	extproducts, err = eansearch.BarcodeLookup("5099750442227", eansearch.English)

	if err != nil {
		fmt.Println(err)
	} else if len(products) == 0 {
		fmt.Println("No results found")
	} else {
		printExtProduct(products[0])
	}

	products, err = eansearch.ISBNLookup("1119578884")

	if err != nil {
		fmt.Println(err)
	} else if len(products) == 0 {
		fmt.Println("No results found")
	} else {
		printProduct(products[0])
	}

	products, more, err = eansearch.ProductSearch("esprit pullover", 0, eansearch.English)
	// products, more, err = eansearch.SimilarProductSearch("esprit whateverelsenotfound pullover", 0, eansearch.English)

	if err != nil {
		fmt.Println(err)
	} else if len(products) == 0 {
		fmt.Println("No results found")
	} else {
		for _, p := range products {
			printProduct(p)
		}
		if more {
			fmt.Println("More results available")
		}
	}

	products, more, err = eansearch.BarcodePrefixSearch("40620999", 0, eansearch.AnyLanguage)

	if err != nil {
		fmt.Println(err)
	} else if len(products) == 0 {
		fmt.Println("No results found")
	} else {
		for _, p := range products {
			printProduct(p)
		}
		if more {
			fmt.Println("More results available")
		}
	}

	ean := "5099750442227"
	country, err = eansearch.IssuingCountryLookup(ean)

	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("EAN %s was issued in %s\n", ean, country)
	}


        ean = "5099750442228"
        valid, err := eansearch.VerifyChecksum(ean)

        if err != nil {
                fmt.Println(err)
        } else {
                if (valid) {
                        fmt.Printf("EAN %s is VALID\n", ean)
                } else {
                        fmt.Printf("EAN %s is INVALID\n", ean)
                }
        }

        // find the Amazon ASIN for a barcode, and the barcode for an ASIN
        asin, err := eansearch.FindAsinForEan("9781119578888")
        if err != nil {
                fmt.Println(err)
        } else {
                fmt.Println("ASIN:", asin)
                ean, err = eansearch.FindEanForAsin(asin)
                if err == nil {
                        fmt.Println("EAN for ASIN", asin, "is", ean)
                }
        }

        // find the Library of Congress control number (LCCN) for a barcode, and the barcode for an LCCN
        lccn, err := eansearch.FindLccnForEan("9781119578888")
        if err != nil {
                fmt.Println(err)
        } else {
                fmt.Println("LCCN:", lccn)
                ean, err = eansearch.FindEanForLccn(lccn)
                if err == nil {
                        fmt.Println("EAN for LCCN", lccn, "is", ean)
                }
        }

        ean = "5099750442227"
        image, err := eansearch.BarcodeImage(ean)

        if err != nil {
                fmt.Println(err)
        } else {
                //fmt.Print("Content-Type: image/png\n\n")
                //fmt.Printf("%s", image)
                _ = image
                fmt.Println("Barcode image received")
        }

        credits, err := eansearch.CreditsRemaining()
        if err == nil {
                fmt.Println("API credits remaining:", credits)
        }

}

Documentation

Overview

Package eansearch is a Go module for EAN, GTIN and ISBN name lookup and validation

Index

Constants

View Source
const AnyLanguage uint = 99
View Source
const Danish uint = 2
View Source
const Dutch uint = 10
View Source
const English uint = 1
View Source
const Finish uint = 5
View Source
const French uint = 6
View Source
const German uint = 3
View Source
const Italian uint = 8
View Source
const MaxApiTries uint = 3
View Source
const Norwegian uint = 11
View Source
const Polish uint = 12
View Source
const Portuguese uint = 13
View Source
const Spanish uint = 4
View Source
const Swedish uint = 15

Variables

This section is empty.

Functions

func BarcodeImage added in v1.3.0

func BarcodeImage(ean string) ([]byte, error)

func CreditsRemaining added in v1.6.0

func CreditsRemaining() (int, error)

CreditsRemaining returns the number of remaining API credits

func FindAsinForEan added in v1.6.0

func FindAsinForEan(ean string) (string, error)

FindAsinForEan finds the Amazon ASIN for an EAN or ISBN-13 barcode

func FindEanForAsin added in v1.6.0

func FindEanForAsin(asin string) (string, error)

FindEanForAsin finds the EAN barcode for an Amazon ASIN

func FindEanForLccn added in v1.6.0

func FindEanForLccn(lccn string) (string, error)

FindEanForLccn finds the EAN barcode for a Library of Congress Control Number (LCCN) There can be multiple EANs for one LCCN, this returns the first one found.

func FindLccnForEan added in v1.6.0

func FindLccnForEan(ean string) (string, error)

FindLccnForEan finds the Library of Congress Control Number (LCCN) for an EAN or ISBN-13 barcode

func IssuingCountryLookup added in v1.3.0

func IssuingCountryLookup(ean string) (string, error)

func SetToken

func SetToken(t string) error

SetToken initialises the API with the token, you can apply for at https://www.ean-search.org/ean-database-api.html

func VerifyChecksum added in v1.3.0

func VerifyChecksum(ean string) (bool, error)

Types

type AsinLookup added in v1.6.0

type AsinLookup struct {
	Ean  string
	Asin string
}

type AsinOrError added in v1.6.0

type AsinOrError struct {
	AsinLookup
	Error string
}

type Checksum added in v1.3.0

type Checksum struct {
	Ean   string
	Valid string
}

type ChecksumOrError added in v1.3.0

type ChecksumOrError struct {
	Checksum
	Error string
}

type ExtProduct added in v1.5.0

type ExtProduct struct {
	Ean              string
	Name             string
	CategoryID       uint `json:",string"`
	CategoryName     string
	GoogleCategoryID uint `json:",string"`
	IssuingCountry   string
}

func BarcodeLookup

func BarcodeLookup(ean string, lang uint) ([]ExtProduct, error)

BarcodeLookup searches for a single EAN code

type ExtProductOrError added in v1.5.0

type ExtProductOrError struct {
	ExtProduct
	Error string
}

type Image added in v1.3.0

type Image struct {
	Ean     string
	Barcode string
}

type ImageOrError added in v1.3.0

type ImageOrError struct {
	Image
	Error string
}

type LccnLookup added in v1.6.0

type LccnLookup struct {
	Ean  string
	Lccn string
}

type LccnOrError added in v1.6.0

type LccnOrError struct {
	LccnLookup
	Error string
}

type Product

type Product struct {
	Ean            string
	Name           string
	CategoryID     uint `json:",string"`
	CategoryName   string
	IssuingCountry string
}

Product holds datasets returned by the API

func BarcodePrefixSearch

func BarcodePrefixSearch(prefix string, page uint, lang uint) ([]Product, bool, error)

BarcodePrefixSearch find all EANs strating with a certain prefix

func CategorySearch added in v1.3.0

func CategorySearch(category uint, name string, page uint, lang uint) ([]Product, bool, error)

CategorySearch searches for products by category and name

func ISBNLookup added in v1.3.0

func ISBNLookup(isbn string) ([]Product, error)

ISBNLookup searches for a single ISBN-10 or ISBN-13 code

func ProductSearch

func ProductSearch(name string, page uint, lang uint) ([]Product, bool, error)

ProductSearch searches for products by name (exact)

func SimilarProductSearch added in v1.4.0

func SimilarProductSearch(name string, page uint, lang uint) ([]Product, bool, error)

SilimarProductSearch searches for products by name (similar name is also ok)

type ProductOrError added in v1.3.0

type ProductOrError struct {
	Product
	Error string
}

Jump to

Keyboard shortcuts

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