Hi there! I'm Shrijith Venkatrama, founder of Hexmos. Right now, I’m building LiveAPI, a tool that makes generating API docs from your code ridiculously easy.
Let’s dive into go-pretty—a neat little Go library that turns your boring console output into something way more fun and readable.
Think tables with colors, progress bars that actually look cool, and lists that don’t make your eyes hurt.
I’ve been messing around with it, and it’s definitely a game-changer for CLI tools.
Let’s break it down into some hands-on sections so you can see what it’s all about.
We’ll keep it simple, and packed with code you can try yourself.
Getting Started with go-pretty
First things first, you need to grab the library. Open your terminal and run this:
go get github.com/jedib0t/go-pretty/v6
Done? Cool. Now, let’s import what we need. For this blog, we’ll play with tables, progress bars, and lists, so here’s a basic setup in your main.go:
package main
import (
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/list"
"github.com/jedib0t/go-pretty/v6/progress"
)
func main() {
// We’ll fill this in soon!
}
That’s it for setup. Super quick, right? The library’s got a bunch of packages—table, list, progress, and text—and we’ll hit the highlights step-by-step.
Building a Fancy Table
Tables in the console usually suck. Plain text, misaligned columns, bleh. With go-pretty, you can make them look sharp. Let’s whip up a simple table of some fictional employees.
Here’s the code:
package main
import (
"os"
"github.com/jedib0t/go-pretty/v6/table"
)
func main() {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Name", "Job", "Salary"})
t.AppendRows([]table.Row{
{1, "Alice", "Developer", 60000},
{2, "Bob", "Designer", 55000},
})
t.AppendFooter(table.Row{"", "", "Total", 115000})
t.SetStyle(table.StyleLight)
t.Render()
}
Run it with go run main.go, and you’ll see something like this:
┌──┬───────┬────────────┬────────┐
│ #│ Name │ Job │ Salary │
├──┼───────┼────────────┼────────┤
│ 1│ Alice │ Developer │ 60000 │
│ 2│ Bob │ Designer │ 55000 │
├──┼───────┼────────────┼────────┤
│ │ │ Total │ 115000 │
└──┴───────┴────────────┴────────┘
The StyleLight
gives it those clean lines, but you can swap it for StyleColoredBright
if you want some color action. Play with it—add more rows, tweak the style, see what sticks.
Tracking Stuff with Progress Bars
Ever wanted to show progress in your CLI app without it looking like a mess? The progress package has your back. Let’s simulate downloading two files.
Here’s a quick example:
package main
import (
"time"
"github.com/jedib0t/go-pretty/v6/progress"
)
func main() {
pw := progress.NewWriter()
pw.SetOutputWriter(os.Stdout)
tracker1 := pw.AppendTracker(progress.Tracker{Name: "File1.zip", Total: 100})
tracker2 := pw.AppendTracker(progress.Tracker{Name: "File2.zip", Total: 100})
go pw.Render()
for i := 0; i < 100; i += 10 {
time.Sleep(500 * time.Millisecond)
tracker1.Increment(10)
tracker2.Increment(10)
}
time.Sleep(time.Second) // Let it finish rendering
}
Run this, and you’ll see two progress bars ticking up together.
It’s smooth, it’s visual, and it beats printing percentages manually.
The go pw.Render()
runs it in the background, so your main logic keeps chugging along.
Try adding more trackers or messing with the Total value to see how it scales.
Pretty Lists That Don’t Suck
Lists in the console can get messy fast, especially with nesting. The list package makes it painless. Let’s make a little hierarchy of tasks.
Check this out:
package main
import (
"os"
"github.com/jedib0t/go-pretty/v6/list"
)
func main() {
l := list.NewWriter()
l.SetOutputMirror(os.Stdout)
l.AppendItem("Project Kickoff")
l.Indent()
l.AppendItem("Plan")
l.AppendItem("Code")
l.Indent()
l.AppendItem("Frontend")
l.AppendItem("Backend")
l.UnIndent()
l.AppendItem("Launch")
l.UnIndent()
l.SetStyle(list.StyleConnectedLight)
l.Render()
}
Output looks like this:
╭─ Project Kickoff
│ ├─ Plan
│ ├─ Code
│ │ ├─ Frontend
│ │ ╰─ Backend
│ ╰─ Launch
The StyleConnectedLight
gives it those nice connecting lines. You can nest deeper or switch to StyleBulletCircle
for a different vibe. It’s perfect for showing structure without cluttering things up.
Why You Should Care
So, why bother with go-pretty?
If you’re building a CLI app—or even just debugging with some output—this library saves you from reinventing the wheel. The customization is wild too.
Want colored tables? Done. Animated progress? Easy. Nested lists with style? You got it.
Start small.
Throw a table into your next project. Then maybe a progress bar.
Before you know it, your console output will be the envy of your dev friends. Check the go-pretty docs for more tricks—I barely scratched the surface here.
Top comments (0)