Adding Color to Terminal Output in Go

GOLANG
2 min read

When working with command-line applications in Go, color can be a great way to enhance the user experience, convey information in a more visually appealing way, or generally communicate more effectively. In this article, I'll show you some easy ways to get started incorporating color into your Go code!

ANSI escape codes

The most direct and lightweight way to add color to your output is to use ANSI escape codes. ANSI escape codes are sequences of characters which, when printed to the terminal, control various text formatting options, including color.

var Reset = "\033[0m" 
var Red = "\033[31m" 
var Green = "\033[32m" 
var Yellow = "\033[33m" 
var Blue = "\033[34m" 
var Magenta = "\033[35m" 
var Cyan = "\033[36m" 
var Gray = "\033[37m" 
var White = "\033[97m"

All you need to do is prepend the appropriate sequence to the beginning of your text and append the reset sequence afterwards to set the color back to the default.

println(Red + "This is Red" + Reset) 
println(Green + "This is Green" + Reset) 
println(Yellow + "This is Yellow" + Reset) 
println(Blue + "This is Blue" + Reset) 
println(Magenta + "This is Purple" + Reset) 
println(Cyan + "This is Cyan" + Reset) 
println(Gray + "This is Gray" + Reset)
println(White + "This is White" + Reset) 

all colors

You can also adjust various other settings such as the brightness of the text.

println("\033[33;1m This is Bright Yellow \033[0m")  
println("\033[33m This is Yellow \033[0m")

bright dim

We use colors all over our Dolt Command Line Interface, but one place it's going to be especially relevant is in our upcoming support for dolt log --graph. Here's a sneak peak:

log graph example

Colors are really useful here to help visually keep track of all the extended and potentially intersecting lines in your commit log graph.

Lastly, remember to account for any ANSI escape codes you added when doing things like string matching. You might need to use strings.Contains() instead of an equals operator. For us, we also need to make sure to strip out any special characters when doing output matching in our BATS tests. For example, a common pattern you'll see in our tests is stripping out special characters using echo "$output" | sed -E 's/\x1b\[[0-9;]*m//g'.

fatih/color

Working directly with ANSI codes can get a little messy. We recommend the library that we use in Dolt, which is fatih/color. It provides a great wrapper making it much easier and cleaner to add color to your output.

println(color.RedString("This is Red")) 
println(color.GreenString("This is Green")) 
println(color.YellowString("This is Yellow")) 
println(color.BlueString("This is Blue")) 
println(color.MagentaString("This is Purple")) 
println(color.CyanString("This is Cyan"))

This package also has a lot of other useful functions like being able to mix and reuse colors or create custom print functions.

That's all for today! Let us know how you use colors in your command-line application, or come talk Go with us by dropping by our discord server!

SHARE

JOIN THE DATA EVOLUTION

Get started with Dolt

Or join our mailing list to get product updates.