How to generate QR codes and barcodes from data in Go?

To generate QR codes and barcodes from data in Go, you can use external libraries or built-in packages. Here's an example of generating QR codes and barcodes using popular libraries:

  1. Generating QR codes using the "go-qrcode" library:

    • Install the library using the following command:

      go get github.com/skip2/go-qrcode
    • Use the following code snippet to generate a QR code and save it to a file:

      package main import ( "github.com/skip2/go-qrcode" "log" ) func main() { data := "Hello, World!" file := "qrcode.png" err := qrcode.WriteFile(data, qrcode.Medium, 256, file) if err != nil { log.Fatal(err) } log.Println("QR code generated successfully!") }

    Running this code will generate a QR code image ("qrcode.png") containing the data "Hello, World!".

  2. Generating barcodes using the "github.com/boombuler/barcode" library:

    • Install the library using the following command:

      go get github.com/boombuler/barcode
    • Use the following code snippet to generate a barcode and save it to a file:

      package main import ( "github.com/boombuler/barcode" "github.com/boombuler/barcode/qr" "image/png" "log" "os" ) func main() { data := "Hello, World!" file := "barcode.png" qrCode, err := qr.Encode(data, qr.M, qr.Auto) if err != nil { log.Fatal(err) } qrCode, _ = barcode.Scale(qrCode, 256, 256) fileHandle, _ := os.Create(file) defer fileHandle.Close() png.Encode(fileHandle, qrCode) log.Println("Barcode generated successfully!") }

    Running this code will generate a barcode image ("barcode.png") containing the data "Hello, World!".

There are other libraries available for generating QR codes and barcodes in Go, so feel free to explore and use the one that suits your requirements the best.