Golang color terminal output with fatih/color

Author

Gurleen Sethi on 19 February 2023

Golang color terminal output with fatih/color

Introduction #

fatih/color package allows you to print colored text in the terminal using Go.

To learn about this library, let's assume an example of a CLI application that performs certain tasks and prints out a summary.

Project Setup #

Create a new Go project and install the fatih/color package.

go get github.com/fatih/color

Basic Colors #

Below is the most basic way to print out colored text.

package main

import (
	"fmt"

	"github.com/fatih/color"
)

func main() {
	completed := 5
	failed := 3
	warnings := 2

	fmt.Printf("\n\n=== Task results ===\n\n")

	// 👇👇👇
	color.Green("%d tasks completed", completed)
	color.Red("%d tasks failed", failed)
	color.Yellow("%d tasks produced warnings", warnings)

	fmt.Println()
}
main.go

Execute the code above using go run main.go, and you will receive the following output.

Golang fatih color basic colors

List of available colors: Black, Red, Green, Yellow, Blue, Magenta, Cyan, White.

Background color #

You can also add background color to the text, to achieve this you need to create a new color using color.New.

package main

import (
	"fmt"

	"github.com/fatih/color"
)

func main() {
	completed := 5
	failed := 3
	warnings := 2

	yellow := color.New(color.FgHiWhite, color.BgYellow) // 👈

	fmt.Printf("\n\n=== Task results ===\n\n")

	color.Green("%d tasks completed", completed)
	color.Red("%d tasks failed", failed)
	yellow.Printf("%d tasks produced warnings\n", warnings) // 👈

	fmt.Println()
}
main.go

Execute the code above using go run main.go, and you will receive the following output.

golang-fatih-color-background

Colors that start with Fg are meant for the foreground and will affect the text color. On the other hand, colors that start with Bg are intended for the background and will apply a color to the text's background.

Formatting Text #

Bold #

There are many options available to style text. The following example demonstrates how to make text bold.

package main

import (
	"fmt"

	"github.com/fatih/color"
)

func main() {
	completed := 5
	failed := 3
	warnings := 2

	red := color.New(color.FgRed, color.Bold)

	fmt.Printf("\n\n=== Task results ===\n\n")

	color.Green("%d tasks completed", completed)
	red.Printf("%d tasks failed\n", failed)
	color.Yellow("%d tasks produced warnings", warnings)

	fmt.Println()
}
main.go

Execute the code above using go run main.go, and you will receive the following output.

Golang fatih color bold

Italic and underline #

The following example applies bold, italic, and underline formatting to the text.

package main

import (
	"fmt"

	"github.com/fatih/color"
)

func main() {
	completed := 5
	failed := 3
	warnings := 2

	red := color.New(color.FgRed, color.Bold, color.Italic, color.Underline)

	fmt.Printf("\n\n=== Task results ===\n\n")

	color.Green("%d tasks completed", completed)
	red.Printf("%d tasks failed\n", failed)
	color.Yellow("%d tasks produced warnings", warnings)

	fmt.Println()
}
main.go

Execute the code above using go run main.go, and you will receive the following output.

Golang fatih color bold italic underline

Complete list of formatting options: Bold, Faint, Italic, Underline, BlinkSlow, BlinkRapid, ReverseVideo, Concealed, CrossedOut.

Ending #

Do checkout the fatih/color github repository.

Table of Contents
Subscribe via email

Get notified once/twice per month when new articles are published.

Byte Byte Go Affiliate
TheDeveloperCafe
Copyright © 2022 - 2024 TheDeveloperCafe.
The Go gopher was designed by Renee French.