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.