QR Code Generator in Go (Golang) with go-qrcode Library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

QR Code Generator in Go (Golang): go-qrcode Library



QR codes have become an integral part of the modern web — from website links to sharing contact information. If you are developing in Go (Golang) and want to integrate QR code generation into your application, the go-qrcode library is the perfect solution. It is lightweight, written entirely in Go with no external dependencies, and supports all major QR code versions.



Installation and Setup



To get started, install the package using the standard Go module manager:



go get github.com/skip2/go-qrcode


The library requires no additional system libraries — everything works out of the box. Go version 1.13 and higher is supported.



Basic Usage: Creating Your First QR Code



The simplest way to generate a QR code is to use the WriteFile function. It creates a PNG image directly to disk:



package main

import ( "github.com/skip2/go-qrcode")

func main() { err := qrcode.WriteFile("https://pythonlib.ru", qrcode.Medium, 256, "qr.png") if err != nil { panic(err) }}


In this example, we create a QR code for the URL https://pythonlib.ru with an error correction level of Medium and a size of 256x256 pixels. The result is saved to the file qr.png.



Generation Parameters



The WriteFile function takes four arguments:



  • content (string) — the data to encode (text, URL, phone number, etc.)
  • level (RecoveryLevel) — the error correction level. Available values:
    • qrcode.Low — 7% recovery
    • qrcode.Medium — 15% recovery
    • qrcode.High — 25% recovery
    • qrcode.Highest — 30% recovery
  • size (int) — the image size in pixels (typically from 128 to 1024)
  • filename (string) — the path to the output file (the .png extension is required)


Generating a QR Code in Memory (Without Saving to Disk)



If you need to obtain the QR code as PNG bytes (for example, to send via HTTP or save to a database), use the PNG method:



package main

import ( "bytes" "image/png" "log" "github.com/skip2/go-qrcode")

func main() { // Create the QR code qr, err := qrcode.New("https://pythonlib.ru", qrcode.Medium) if err != nil { log.Fatal(err) }

// Get the image as *image.RGBA img := qr.Image(256)

// Encode to PNG and get the bytes var buf bytes.Buffer err = png.Encode(&buf, img) if err != nil { log.Fatal(err) }

// Now buf.Bytes() contains the PNG data log.Printf("QR code generated, size: %d bytes", buf.Len())}


This approach is convenient for web servers: you can serve the QR code directly in an HTTP response without creating temporary files.



Customizing Colors and Background



By default, the QR code is black and white, but the library allows you to change colors. Use the ForegroundColor and BackgroundColor methods:



package main

import ( "image/color" "log" "github.com/skip2/go-qrcode")

func main() { qr, err := qrcode.New("https://pythonlib.ru", qrcode.High) if err != nil { log.Fatal(err) }

// Set blue color for modules and white for the background qr.ForegroundColor = color.RGBA{0, 0, 255, 255} // blue qr.BackgroundColor = color.RGBA{255, 255, 255, 255} // white

// Save with the new colors err = qr.WriteFile(256, "colorful_qr.png") if err != nil { log.Fatal(err) }}

Recommendations