Golang : Progress bar with ∎ character
This tutorial is add on to the previous tutorial on how upload/receive progress indicator. Just a slight modification, where the progress is indicated by ∎ character and % completed.
NOTE : To add % sign after %v, use two % symbols.
Here you go :
package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
)
const (
barChar = "∎"
)
func uploadPage(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("<html><title>Go upload</title><body><form action='http://localhost:8080/receive' method='post' enctype='multipart/form-data'><label for='file'>Filename:</label><input type='file' name='file' id='file'><input type='submit' value='Upload' ></form></body></html>")))
}
func uploadProgress(w http.ResponseWriter, r *http.Request) {
mr, err := r.MultipartReader()
if err != nil {
fmt.Fprintln(w, err)
return
}
length := r.ContentLength
//ticker := time.Tick(time.Millisecond) // <-- use this in production
ticker := time.Tick(time.Second) // this is for demo purpose with longer delay
for {
var read int64
var p float32
part, err := mr.NextPart()
if err == io.EOF {
fmt.Printf("\nDone!")
break
}
dst, err := os.OpenFile("upload.jpg", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return
}
var barLen int
for {
buffer := make([]byte, 100000)
cBytes, err := part.Read(buffer)
if err == io.EOF {
fmt.Printf("\nLast buffer read!")
break
}
read = read + int64(cBytes)
//fmt.Printf("\r read: %v length : %v \n", read, length)
if read > 0 {
p = float32(read*100) / float32(length)
barLen = int(p)
//fmt.Printf("progress: %v \n", strings.Repeat(barChar,barLen))
<-ticker
fmt.Printf("\rStatus: %v [%v%%]", strings.Repeat(barChar, barLen), p)
//fmt.Printf("\rUploading progress %v", p) // for console
dst.Write(buffer[0:cBytes])
} else {
break
}
}
}
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", uploadPage)
mux.HandleFunc("/receive", uploadProgress)
http.ListenAndServe(":8080", mux)
}
Sample output :
go run uploadprogress.go
Status: ∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎ [99.915634%]
Last buffer read!
See also : Golang : Upload/Receive file progress indicator
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+18.7k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+11.8k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+5.9k Golang : Measure execution time for a function
+9.7k Golang : Setting variable value with ldflags
+17.8k Golang : Check if a directory exist or not
+62.4k Golang : Convert HTTP Response body to string
+14.1k Golang : Recombine chunked files example
+7k Golang : Of hash table and hash map
+12k Golang : Simple client-server HMAC authentication without SSL example
+14.3k Golang : GUI with Qt and OpenCV to capture image from camera
+13k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+5.8k PageSpeed : Clear or flush cache on web server