eansearch

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2025 License: MIT Imports: 9 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)
                }
        }

        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")
        }

}

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 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 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 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