Note: If you are interested in learning Golang, then for that we have a golang comprehensive tutorial series. Do check it out – Golang Comprehensive Tutorial Series. Now let’s see the current tutorial. Below is the table of contents.
Overview
ANSI escape codes can be used to output colored text in console. Please note that
- In MAC/Linux system terminal supports ANSI escape codes
- Windows Command Prompt doesn’t support it. On windows you can install Cygwin. ANSI escape codes work on that.
Also to mention in the below code we have used colorReset after printing. If we don’t use that then the color effect remains and it is not cleared. Remove the colorReset from below code and it will show text “next” in cyan color
Below is code in golang to do the same.
Code
package main
import (
"fmt"
)
func main() {
colorReset := "\033[0m"
colorRed := "\033[31m"
colorGreen := "\033[32m"
colorYellow := "\033[33m"
colorBlue := "\033[34m"
colorPurple := "\033[35m"
colorCyan := "\033[36m"
colorWhite := "\033[37m"
fmt.Println(string(colorRed), "test")
fmt.Println(string(colorGreen), "test")
fmt.Println(string(colorYellow), "test")
fmt.Println(string(colorBlue), "test")
fmt.Println(string(colorPurple), "test")
fmt.Println(string(colorWhite), "test")
fmt.Println(string(colorCyan), "test", string(colorReset))
fmt.Println("next")
}
Output:
On my mac machine