How to generate a QR image based on a link with Golang

Jhon Baron
4 min readJan 29, 2023

--

QR code (Quick Response code) is a two-dimensional barcode that can be scanned using a smartphone camera or a dedicated QR code reader. It can store various types of data, including text, URLs, and contact information. QR codes are often used for marketing and advertising purposes, as they can be easily scanned to access a website or a promotional offer. They are also commonly used in inventory management, retail, and other mobile payments, public transportation, and event ticketing applications.

QR Code Use Cases

  1. Bring offline users to your website and landing pages
  2. Share your contact information
  3. Inform and educate users
  4. Create ID tags for pets
  5. QR code menus in restaurants
  6. Increase mobile app downloads
  7. Promote your social media profiles
  8. Share your business information
  9. Share map locations
  10. Smart product packaging
Gopher QR

Generating a QR Code using Golang

This code creates multiple QR codes concurrently and saves them to files. First, the code imports the “fmt”, “sync” and “github.com/skip2/go-qrcode” packages, the “fmt” package is used for formatting strings, the “sync” package is used for synchronizing concurrent execution and the “github.com/skip2/go-qrcode” package is used for generating QR codes.

Next, in the main function:

  • An array of URLs is defined as urls.
  • A WaitGroup wg is created and its counter is set to the length of the urls array.
  • A loop is run over the urls array using the range keyword.
  • Before starting the goroutine generation, the wg.Add(len(urls)) is called to increment the counter by urls size.
  • In each iteration, a goroutine is started using the go keyword.
  • The goroutine calls the generateQR function, passing in the current iteration index i and the current URL url.
  • After starting the goroutine, the defer wg.Done() is called to decrement the counter by 1.
  • Finally, the wg.Wait() function is called to wait for all the goroutines to complete and the counter to reach 0.
package main

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

func main() {
urls := []string{
"www.example.com/1",
"www.example.com/2",
"www.example.com/3",
}
var wg sync.WaitGroup
wg.Add(len(urls))
for i, url := range urls {
url := url
i := i
go func() {
defer wg.Done()
generateQR(i, url)
}()
}
wg.Wait()
}

func generateQR(i int, url string) {
qrCode, _ := qrcode.New(url, qrcode.Medium)
fileName := fmt.Sprintf("%v.png", i)
err := qrCode.WriteFile(256, fileName)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(fmt.Sprintf("QR code generated and saved as %v.png", i))
}

The generateQR function takes in two arguments, an integer i and a string url.

  • This function uses the qrcode.New function from the "github.com/skip2/go-qrcode" package to create a new QR code with the passed-in url and the error correction level qrcode.Medium.
  • It then uses the qrCode.WriteFile function to write the QR code to a file with the specified filename and size 256x256. You can change the data encoded in the QR code as per your requirement and also the file name to save it. If there is any error while writing the file it will print the error message.
  • If the QR code is generated and saved successfully, it prints a message with the filename of the saved QR code.

An example of this little exercise:

www.example.com/ 2 QR Code.

Conclusion

To conclude this simple post, one could highlight the key features and benefits of the code.

First, the code demonstrates the use of Go’s concurrency features to generate multiple QR codes, making it more efficient than doing it sequentially. This can be useful for generating a large number of QR codes quickly.

Second, the code shows how to use the “github.com/skip2/go-qrcode” package to create and write QR codes to files. You can reach out in Github about another dependency/one if you like another implementation that fits your use case.

Finally, the code also shows how to use the “fmt” and “sync” packages for formatting strings and synchronizing concurrent execution.

Overall, this code is a useful example for anyone looking to generate QR codes in Go, especially when working with a large number of QR codes and looking for an efficient way to generate them.

Donation

Donations help me to continue providing valuable information and assistance to users like you. With your support, I can continue to improve and expand my knowledge base to better serve the community.

To donate, simply click on the PayPal link provided and select the amount you would like to contribute. PayPal is a safe and secure way to make a donation, and you can use it even if you don’t have an account. You can also scan this QR code for easy mobile PayPal donation:

Jhon Baron PayPal Donation QR

Your support is truly appreciated, and I thank you for considering a donation. Every little bit helps, and your generosity will help me to continue providing helpful and accurate information to users like you.

--

--