How to Add Progress Bars to a Go CLI

Cli
Add a visual progress bar to a Go CLI using the schollz/progressbar library to track task completion.

Use the github.com/schollz/progressbar/v3 library to render a visual progress bar in your Go CLI. Install the package, create a new bar with your total count, and update it inside your loop.

package main

import (
	"fmt"
	"time"
	"github.com/schollz/progressbar/v3"
)

func main() {
	bar := progressbar.NewOptions(100, progressbar.OptionSetDescription("Processing..."))
	for i := 0; i < 100; i++ {
		time.Sleep(50 * time.Millisecond)
		if err := bar.Add(1); err != nil {
			fmt.Println(err)
		}
	}
}

Run go get github.com/schollz/progressbar/v3 to install the dependency.