Generate QR Codes with Go
Go is ideal for high-performance QR code generation in microservices and CLI tools. Use the go-qrcode library for local generation or the QRCode.fun API for styled outputs.
Installation
Install the Go QR code library using go get.
go get github.com/skip2/go-qrcodeGenerate QR Codes with Go
Code examples using the go-qrcode library.
Basic QR Code Generation
package main
import "github.com/skip2/go-qrcode"
func main() {
err := qrcode.WriteFile("https://qrcode.fun", qrcode.Medium, 256, "qrcode.png")
if err != nil {
panic(err)
}
}QR Code as Bytes (HTTP Handler)
package main
import (
"net/http"
"github.com/skip2/go-qrcode"
)
func qrHandler(w http.ResponseWriter, r *http.Request) {
data := r.URL.Query().Get("data")
if data == "" {
data = "https://qrcode.fun"
}
png, err := qrcode.Encode(data, qrcode.Medium, 256)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "image/png")
w.Write(png)
}
func main() {
http.HandleFunc("/qr", qrHandler)
http.ListenAndServe(":8080", nil)
}Generate QR Codes via API in Go
Call the QRCode.fun API from Go to generate styled QR codes.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
payload := map[string]interface{}{
"data": "https://qrcode.fun",
"width": 300,
"height": 300,
"type": "png",
"margin": 10,
"dotsOptions": map[string]string{
"color": "#1A2B3C",
"type": "rounded",
},
"cornersSquareOptions": map[string]string{
"color": "#8564C3",
"type": "extra-rounded",
},
"backgroundOptions": map[string]string{
"color": "#FFFFFF",
},
}
body, _ := json.Marshal(payload)
resp, err := http.Post(
"https://qrcode.fun/api/generate-qr-styled",
"application/json",
bytes.NewBuffer(body),
)
if err != nil {
panic(err)
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
fmt.Println(string(data[:100]))
}Live QR Code Preview
Try generating a QR code with Go right now.
Native Library vs API
Compare using a Go QR code library versus the QRCode.fun API.
| Feature | Native Library | QRCode.fun API |
|---|---|---|
| Setup complexity | go get + import | Single HTTP request |
| Customization | Size, error correction | Full styling: colors, shapes, logos |
| Offline support | Yes | Requires internet |
| Maintenance | Update go.mod | Always up-to-date |
| Output formats | PNG | PNG, SVG |
Go QR Code Use Cases
Common scenarios for QR codes in Go applications.
Microservices
Generate QR codes in Go microservices for API responses, webhooks, and event-driven architectures.
CLI Tools
Build command-line utilities that generate QR codes for Wi-Fi sharing, URL shortening, or terminal display.
Cloud Functions
Deploy QR generation as serverless functions on AWS Lambda or Google Cloud Functions using Go.
DevOps & Infrastructure
Generate QR codes for deployment URLs, monitoring dashboards, and configuration endpoints.
Go QR Code Ecosystem Deep Dive
Go's simplicity and performance make it ideal for QR code generation in cloud-native and high-throughput environments.
Microservice Architecture with QR Generation
Go excels at building dedicated QR code microservices. Deploy a lightweight HTTP service using net/http or Gin that accepts QR parameters and returns PNG bytes. Go's goroutine model handles thousands of concurrent QR generation requests with minimal memory. Combine with gRPC for internal service-to-service QR generation, or expose a REST API behind an API gateway for external consumers.
Docker & Kubernetes Deployment
Go's single-binary compilation makes QR generation services trivial to containerize. Build a scratch-based Docker image under 10MB that includes your QR service with zero dependencies. Deploy to Kubernetes with horizontal pod autoscaling based on CPU usage during peak QR generation loads. The go-qrcode library has no CGO dependencies, ensuring clean cross-compilation for linux/amd64 and linux/arm64 targets.
Standard Library Integration & Testing
Go's standard library complements QR code generation perfectly. Use image/png and image/draw for custom rendering beyond what go-qrcode provides. The net/http package serves generated QR codes directly without a framework. Go's built-in testing and benchmarking tools (go test -bench) let you measure exact generation throughput — typically 5,000-10,000 QR codes per second per core for standard configurations.
Frequently Asked Questions
Common questions about generating QR codes with Go.
Start generating QR codes with Go
Use our free generator or integrate the API into your Go services.