In the previous tutorial we saw how to generate and decode QR-code using python. In this tutorial we will see how to do the same using a different language precisely – Golang/Go.
By the end of this tutorial you will learn about
- What QR-Codes are
- How to generate QR-Code in Golang
- Tools/Packages for generating QR-Code
- etc
Let us start.
QR- Codes – What are they?
QR-codes a type of matrix barcode (or two-dimensional barcode) invented in 1994 by the Japanese automotive company. The QR Stands for Quick Response
Packages For Generating QR-code
In Golang, there exist several libraries/packages for working with QR-Code, among them include
- github.com/skip2/go-qrcode
- grcode
In this tutorial we wil use go-qrcode from skip2 for our task. To install this package we can do so globally but let us follow best practices.
We will be using go.mod to generate a workspace with the requirements /dependencies to keep track of the packages and libraries used.
First let us install our packages
go mod init
go get -u github.com/skip2/go-qrcode
How to Generate QR-Code
There are several ways but we will be using just these two. For the first method we will use the WriteFile to generate and write it out. For the next method we will use WriteColorFile. Below is the complete code
package main
import (
"fmt"
"github.com/skip2/go-qrcode"
)
func main() {
err := qrcode.WriteFile("hello this is qrcode in golang", qrcode.Medium, 256, "myfirst_file.png")
if err != nil {
fmt.Printf("Sorry couldn't create qrcode:,%v", err)
}
}
For the next method with the WriteFIleColor interface it will look like this
package main
import (
"fmt"
"image/color"
"github.com/skip2/go-qrcode"
)
func main() {
err := qrcode.WriteColorFile("this is colored", qrcode.Medium, 256, color.Black, color.White, "secondfile.png")
if err != nil {
fmt.Printf("Sorry couldn't create qrcode:,%v", err)
}
}
To run our files we can use either run or build functions eg.
go run main.go
You have seen how easy it is to generate QR code using Go-QRCode. TO get more on working with Golang, you can check out some other materials and resource here You can check out the video tutorial below.
Thanks For Your Time
Jesus Saves
By Jesse E.Agbe(JCharis)