Please purchase the course to watch this video.

Full Course
The fatih/color package offers Go developers a powerful and user-friendly way to add rich color and text styles to CLI application outputs. Building on the foundational concepts of ANSI escape codes, this package simplifies styling by handling common challenges such as compatibility with Windows terminals and automatic detection of TTY interfaces versus plain file outputs. Developers can easily print colored and styled text—using built-in colors, custom RGB values, or combining attributes like bold, underline, and background colors—often via intuitive functions modeled after Go’s familiar fmt package. This flexibility enhances the clarity and usability of CLI applications, making important messages stand out or errors more noticeable, while also ensuring style codes are only used where appropriate. Exploring and integrating this package can significantly improve the visual feedback and overall user experience of command-line tools.
No links available for this lesson.
Over the course of the next few lessons, we're going to take a look at some popular packages when it comes to UI components for building CLI applications in Go, with the first package that we're going to take a look at being the fantastic color package by Fatih.
This package is actually very similar to what we built in the last module when we took a look at how we can perform color and text styling using ANSI escape codes. In this case, this package basically does everything for us, but it provides a few other features that we hadn't managed to implement, including support for Windows, as well as also checking to see whether the output that we write to is going to be a TTY interface.
Setting Up the Demo
In order to take a look at how this package works, I'm going to go ahead and actually use it with my create command, which I've actually slimmed down for this video. Believe it or not, this is my second attempt at this video, and the first time I had my full create command, which opened up a text editor working, everything else, it ended up being a bit of a mess. So I'm going to go ahead and do it this time with a bit of a slimmed down version.
If I go ahead and run this code using the following go run . posts create
command, which runs my create action, passing in the title of "my new post". As you can see, it goes and prints out that a new post was created with the slug of "my-new-post". This occurs here where I'm doing the following string replace, in order to replace spaces with dashes and set it to be a lowercase string.
In our case, however, it might be nice to let the user know that something happened with different colored text. Let's say we go ahead and have the actual slug of the post be printed out in green, and maybe we could do say blue for the "new post created", or something similar. So let's take a look at how we can use the color package in order to achieve this.
Installing the Color Package
To begin, we first need to add the package to our go.mod, which I can do by copying in the following go get command and running it in the terminal:
go get github.com/fatih/color
As you can see, it adds in the color package as well as updating a couple of other dependencies that the color package uses, such as go-isatty
and go-colorable
.
Basic Color Usage
Now in order to use this, we can first go ahead and import this package, which we can do using github.com/fatih/color
. Actually, this needs to be above. Now we can actually make use of it in order to print out a colored string.
The easiest way to use the color package is just to replace the fmt.Println
function with the color.
whichever color you want to print out. In this case, I want to print out green. Because this does an fmt.Println
under the hood or printf
under the hood, we need to use a format string as follows, and we should now be able to print out "new post created" in a green string:
color.Green("New post created with slug: %s\n", slug)
If I go ahead and run this code, you can see that's the case. The output is now being printed out in green. Additionally, if you'll remember back to when we implemented this ourselves in the last module, we also had to reset the ANSI escape codes to make sure that it wouldn't print out the color green for other subsequent console printouts. So let's go ahead and just do say "test" here. This line here should not be printed out with the green color, which it isn't. As you can see, it's printed out with my standard text color. So pretty good.
Selective Coloring
However, as I mentioned before, what if we want to go ahead and actually print this out, just this slug part in green text, and either leave this as the same text color or printed out as a different one. Well, in order to do that, let's go ahead and revert back to the print line statement I was using before.
Rather than using the color.Green
, which is a helper function to print out the entire string or format string in green, we can instead use, say, the color.GreenString
function, which again takes a format string, but will print it out in the color green:
fmt.Printf("New post created with slug: %s\n", color.GreenString(slug))
Now, if I go ahead and run this, you can see that the "new post created" is in the same text colors before, but "my-new-post" is now printed out as green. And we can also go ahead and use this approach to say color the "new post created". Let's go ahead and use BlueString
, which we can do, and we just wrap the "new post created" text in this blue string function:
fmt.Printf("%s with slug: %s\n",
color.BlueString("New post created"),
color.GreenString(slug))
Which if I go ahead and run, you can see the "new post created" is being printed out in blue, and the "my-new-post" slug, which if I go ahead and change to say "my next post", that's also going to be printed out in green as well. Very cool.
Custom Colors with RGB
As well as being able to print out in kind of the standard colors that are defined, we can also make use of this package in order to print out in other colors as well. To achieve this, we can go ahead and actually define a color. Let's go ahead and capture it in a variable called c
. And we can make use of the RGB
function in order to define a new foreground color in 24-bit RGB, which is passed in as integers. So you would do kind of 255, 255, 255
if you wanted white, etc, etc.
So in this case, let's go ahead and do 255
for red. Actually, let's go ahead and do 0
for red. And we'll do 255
for green and 255
for blue. This should give us a nice kind of cyan/teal color. It might be a little bit strong, but we'll try it anyway:
c := color.New(color.FgRGB(0, 255, 255))
Then in order to use this, we have a couple of different approaches we could take. We can either use Print
, which will allow us to print it out to the console, Printf
, Println
, similar to what the fmt package defines, or we can use the Sprintf
, or the Sprint
function, which will return a function that we can use kind of as an sprintf method. So let's go ahead and actually make use of SprintfFunc
as follows. And we can do cf
, I guess, cfunc
. We'll keep this as a c
for the moment. Just remember that c
is no longer a color, it's actually a function:
c := color.New(color.FgRGB(0, 255, 255)).SprintfFunc()
fmt.Printf("New post created with slug: %s\n", c(slug))
So we can just go ahead and pass in c
as follows. And now if I go ahead and run this, we should see this being printed out in a teal-like color, which it is. A very vibrant teal at that.
By using the RGB function, you can pretty much define any color that you like, which is really awesome. This function only works, however, if your terminal supports 24-bit color, which most of them do.
TTY Detection and File Output
However, in some cases, we're not even going to be printing out to a terminal at all. This can be the case if we happen to be writing to a log file, where we don't want the ANSI escape characters to be written as well. Fortunately, the color package is able to discern whether or not it's printing to a terminal text editor or terminal interface compared to just a normal log file.
For example, if I go ahead and do > foo.txt
, foo.txt
actually, not do foo.txt, and print it out, you can see there's no ANSI escape characters being written here either. So in this case, you can see it works both for being able to work in a terminal, but it won't affect your log output either.
Advanced Styling with Attributes
In addition to being able to specify color, you can also specify other color attributes as well, or other text, I guess, attributes. For example, let's go ahead and use the New
function rather than the RGB function. This constructor method will create a new color, but as you can see, it takes a variadic parameter of different color attributes.
So let's say we want to go ahead and print this out as a green color. We can do this using color.FgGreen
, which stands for foreground green. In this case, we're setting the foreground color to be green, but we could set it to be pretty much anything we want. However, let's say we also want to go ahead and add in the attribute of an underlined string. Well, fortunately, we can do this by using the Underline
constant provided by the color package:
c := color.New(color.FgGreen, color.Underline)
Then in order to use this, we can make use of the Sprint
function, and we can just go ahead and replace the color.GreenString
, which we're using to print out the slug with a call to the sprinter function, or the variable c
as we captured it in this case. Oh, we'll also need to go ahead and change the "new post created". Let's go ahead and set this to be the blue string. I actually liked that for this, although we could set it to be maybe a teal string, cyan string. Let's do cyan string for the moment.
Okay, now if we go ahead and run this, passing in the title of "styled blog", we should see the slug come through with a green foreground color and an underlined text, which, as you can see, is the case.
Of course, we can also add other color attributes to this as well. So we could go ahead and do Bold
and then maybe even do color.BgWhite
. Let's do a background of white, see what this looks like:
c := color.New(color.FgGreen, color.Underline, color.Bold, color.BgWhite)
Now, if we go ahead and run this, you can see we get very unreadable, but we get the background of white. This is bold. It's slightly hard to see in this font. Let's go ahead and maybe change this foreground color to be, let's go ahead and change it to be blue, although that might not be readable either. Let's go ahead and do FgHiBlue
maybe. Maybe that's going to be a little bit easier to see. And if we go ahead and run this, not easy at all, let's do FgHiBlack
. FgBlack
maybe. Okay, that's a little easier:
c := color.New(color.FgBlack, color.Underline, color.Bold, color.BgWhite)
As you can see, we're now printing this out with a white background attribute, the underlined attribute, the bold attribute and the foreground color of black.
Summary and Benefits
By using the Fatih color package, you can combine colors in a different way to be able to style text however you like. And because you can create both custom colors, but also make use of the functions that it provides, it allows you a lot of flexibility in how you may want to use the package. Additionally, because it's inspired by the fmt package or the functions provided by the fmt package, then it feels very natural to use.
Homework
In any case, that covers the basic overview of the color package. As I mentioned, there's a lot of really good documentation about how you can use this package when it comes to your own code. So I recommend as homework to go ahead and actually take a look at this documentation in order to see how you may want to use it when it comes to your own CMS application.
Once you've taken a look at the documentation and you can see everything that it provides, the next thing I recommend is to go ahead and actually use this color package inside of your CMS. So being able to:
- Print out red error text in case something goes wrong
- If you happen to have any links inside of your CMS application, printing them out with the underscore color line
- Putting out any information or success or warning messages in their respective colors as well
Once you've managed to complete that, we're then ready to move on to the next lesson where we're going to take a look at some more UI components, specifically progress bars and loading spinners.
No homework tasks for this lesson.