Golang : How to add color to string?
Back in the days of DOS ( before GUI was popular), programmers use colors to decorate text based application to enhance usage experience.
Those with in-depth knowledge of assembly language usually ended up creating their own Graphical User Interface components or use third party library. I still remember that I have to invoke the mouse cursor via the DOS interrupt 33 and provide the mouse cursor glyph. Anyway, this tutorial won't be touching on how to use GUI with Golang. (I write another tutorial for that one day), but will show you how to add color to your string.
There is a Golang package that allows you to add color to string easily and you can get it from https://github.com/mgutz/ansi
with go get -u github.com/mgutz/ansi
and once you have included the mgutz/ansi
package...... the code to use them is pretty straightforward.
For example :
package main
import (
"fmt"
"github.com/mgutz/ansi"
"time"
)
func main() {
// style format :
// "foregroundColor+attributes:backgroundColor+attributes"
// For example : red blinking on white background
ansi.Color("%v your text", "red+B:white")
// colorize a string, SLOW
msg := ansi.Color("foo", "red+b:white")
fmt.Println(msg)
msg2 := ansi.Color("foo", "yellow")
fmt.Println(msg2)
underlineRed := ansi.ColorFunc("red+u")
fmt.Printf(underlineRed("text") + "\n")
// add color to log output example
cyan := ansi.ColorFunc("cyan+")
fmt.Printf(cyan("%v ▶ debug ******* \n"), time.Now())
magenta := ansi.ColorFunc("magenta+")
fmt.Printf(magenta("%v ▶ info ******* \n"), time.Now())
yellow := ansi.ColorFunc("yellow+")
fmt.Printf(yellow("%v ▶ notification ******* \n"), time.Now())
red := ansi.ColorFunc("red+")
fmt.Printf(red("%v ▶ WARNING : Crashing soon! \n"), time.Now())
// cache escape codes and build strings manually
lime := ansi.ColorCode("green+h:black")
reset := ansi.ColorCode("reset")
fmt.Println(lime, "Bring back the 80s!", reset)
}
For more color codes, please see the official documentation at https://github.com/mgutz/ansi
UPDATE : Do check out this package as well https://github.com/fatih/color
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+14.1k Elastic Search : Mapping date format and sort by date
+5.3k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+6.9k Golang : Takes a plural word and makes it singular
+7k Golang : Validate credit card example
+6.2k Golang : Selection sort example
+11k Golang : Simple image viewer with Go-GTK
+11k Golang : Fix - does not implement sort.Interface (missing Len method)
+7.4k Golang : Detect sample rate, channels or latency with PortAudio
+9.6k Golang : interface - when and where to use examples
+6.9k Golang : How to call function inside template with template.FuncMap
+10.2k Golang : cannot assign type int to value (type uint8) in range error
+13.4k Golang : Get user input until a command or receive a word to stop