QR Code Generator in Go (Golang): 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

go-qrcode Library: Generating QR Codes in Go (Golang)



QR codes have become an integral part of the modern web and mobile applications. They are used for quick link navigation, transferring contact information, authentication, and much more. If your project is written in Go (Golang), one of the best libraries for creating QR codes is go-qrcode. In this article, we will thoroughly examine its capabilities, installation, and usage examples.



What is go-qrcode?



go-qrcode is a pure implementation of a QR code generator in the Go language. It has no external dependencies (other than the standard library) and supports all major features: configuring size, error correction level, colors, and background. The library can generate both PNG images and return raw binary data for further processing.



Installation



Installing the library is done with the standard go get command:



go get github.com/skip2/go-qrcode


After that, you can import it into your project:



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


Main Features



  • PNG image generation — the most popular use case.
  • Error correction level configuration — Low, Medium, High, Highest.
  • Size adjustment — from 21x21 to 177x177 modules.
  • Color customization — you can set the foreground and background colors.
  • Unicode support — you can encode any text, including Cyrillic.
  • Raw format output — a byte array for custom processing.


Code Examples



1. Simple QR Code



Let's generate a QR code for a URL and save it to a file:



package main

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

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


In this example, we use the medium error correction level (qrcode.Medium) and an image size of 256x256 pixels.



2. Color Customization



If you need a non-standard QR code color, use the qrcode.Config structure:



package main

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

func main() { qr, err := qrcode.New("Hello, world!", qrcode.Highest) if err != nil { panic(err) }

// Color configuration qr.ForegroundColor = color.RGBA{R: 0, G: 150, B: 255, A: 255} // blue qr.BackgroundColor = color.RGBA{R: 255, G: 255, B: 255, A: 255} // white

err = qr.WriteFile(256, "custom_qrcode.png") if err != nil { panic(err) }}


3. Getting PNG as Bytes



Sometimes you need not to save a file, but to serve the image via HTTP or process it further. For this, use the PNG() method:



package main

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

func main() { qr, err := qrcode.New("https://example.com", qrcode.Low) if err != nil { panic(err) }

pngBytes, err := qr.PNG(256) if err != nil { panic(err) }

fmt.Printf("PNG size: %d bytes", len(pngBytes)) // Now pngBytes can be sent in an HTTP response or saved}


4. Generation with Minimum Size



If you want to get the smallest possible QR code, use the ToQRTerminal() method for terminal debugging, or set the minimum module size:



package main

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

func main() { qr, err := qrcode.New("1234567890", qrcode.High) if err != nil { panic(er

Recommendations