Write a Simple Progress Bar Program Using Golang

In this following tutorial you will see together how to implement in Golang a simple and beauty progress bar program for a the command line application.

#1. What is a Progress Bar?

Progress bars are a graphical representation of the progress of a given task. They can be used in many areas, such as software, games, and web pages.

#2. What are the Advantages of Using Progress Bar?

There are many advantages to using progress bars. One is that they help users better understand how long it will take for them to complete a task.

Another advantage is that they help users better understand the amount of work required and the time it will take to complete.

#3. Why you Should Use a Progress Bar for your Application?

Progress bars are often used in web design to show how much of a website has loaded, or to indicate that an operation is being performed, for example uploading a file or downloading data.

The purpose of this article is to gives you a simple way to implement a progress bar for your console application using Golang.

#4. Progress Bar Program

package main

import (
	"fmt"
	"time"
)

var processBar = []string{
	"00%: [                                          ]",
	"05%: [##                                        ]",
	"10%: [####                                      ]",
	"15%: [######                                    ]",
	"20%: [########                                  ]",
	"25%: [##########                                ]",
	"30%: [############                              ]",
	"35%: [##############                            ]",
	"40%: [################                          ]",
	"45%: [##################                        ]",
	"50%: [####################                      ]",
	"55%: [######################                    ]",
	"60%: [########################                  ]",
	"65%: [##########################                ]",
	"70%: [############################              ]",
	"75%: [##############################            ]",
	"80%: [################################          ]",
	"85%: [##################################        ]",
	"90%: [####################################      ]",
	"95%: [######################################    ]",
	"100%:[##########################################]\n",
}

func main() {

	for idx, val := range processBar {
		fmt.Printf("[%d:1H:2K] \r \a%s", idx, val)
		time.Sleep(1 * time.Second)
	}
}
example of a progress bar in Golang

Conclusion

In this article, we have learned about the progress bar and and a simple way to how it can be designed and integrated into our Go application.

@freecoder
@freecoder

With 15+ years in low-level development, I'm passionate about crafting clean, maintainable code.
I believe in readable coding, rigorous testing, and concise solutions.

Articles: 29