Golang Tutorial
Introduction Variables Constants Data Type Convert Types Operators If..Else Switch..Case For Loops Functions Variadic Functions Deferred Functions Calls Panic and Recover Arrays Slices Maps Struct Interface Goroutines Channels Concurrency Problems Logs Files and Directories Reading and Writing Files Regular Expression Find DNS records Cryptography Gotchas in Golang Import and Export Best Golang Packages Web Application Goroutines and Channels Exercises Reflection in Golang Golang for beginners Strings in Golang HTTP Client Server Examples Context PackageGolang Reference
Basic Programs Advance Programs Data Structure and Algorithms Date and Time Slice Sort, Reverse, Search Functions String Functions Methods and Objects Interface TypeBeego Framework
Beego Setup Beego Database Migration Beego GET POST Beego RoutingWeb Application to generate QR code in Golang
A Quick Response code is a two-dimensional pictographic code used for its fast readability and comparatively large storage capacity. The code consists of black modules arranged in a square pattern on a white background. The information encoded can be made up of any kind of data (e.g., binary, alphanumeric, or Kanji symbols).
The basic example of web application to generate bar-code. The algorithm or internal logic to generate bar-code is available in third-party bar-code package. Here the goal is to show an example how to use a package and create a web-application.
1. Install required package
The Barcode package can be used to create different types of bar-codes. You can install this package by executing the following command in your git bash terminal:
go get github.com/boombuler/barcode
2. Development
2.1 Source code of main.go
The main function begins with a call to http.HandleFunc, which tells the http package to handle all requests to the web root ("/") with homeHandler. The function homeHandler is of the type http.HandlerFunc. It takes an http.ResponseWriter and an http.Request as its arguments.
The function viewCodeHandler that will allow users to view a generated QR-code in new page. It will handle URLs prefixed with "/generator/". The function template.ParseFiles will read the contents of generator.html and return a *template.Template.
package main
import (
"image/png"
"net/http"
"text/template"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
)
type Page struct {
Title string
}
func main() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/generator/", viewCodeHandler)
http.ListenAndServe(":8080", nil)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
p := Page{Title: "QR Code Generator"}
t, _ := template.ParseFiles("generator.html")
t.Execute(w, p)
}
func viewCodeHandler(w http.ResponseWriter, r *http.Request) {
dataString := r.FormValue("dataString")
qrCode, _ := qr.Encode(dataString, qr.L, qr.Auto)
qrCode, _ = barcode.Scale(qrCode, 512, 512)
png.Encode(w, qrCode)
}
The FormValue function will gives the value of dataString input field which will be used to generate the QR code using Encode function.
2.2 Source code of generator.html.
A template file containing the HTML form.
<h1>{{.Title}}</h1>
<div>Please enter the string you want to QRCode.</div>
<form action="generator/" method=post>
<input type="text" name="dataString">
<input type="submit" value="Submit">
</form>
3. Execution
Using command line or putty run command "go run main.go"
You will see an output like the image below.