Skip to content

Latest commit

 

History

History
98 lines (72 loc) · 2.63 KB

README.md

File metadata and controls

98 lines (72 loc) · 2.63 KB

Examples of using color in Go


 

Using Colors in Go

   

Source of the code is HERE

 

Good article about using Color in golang HERE

Simon Tony wrote a Medium article Terminal color render by Golang

 


Using Colors in Go
 

Define like this:

package color

import "runtime"

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


func init() {
	if runtime.GOOS == "windows" {
		Reset   = ""
		Red     = ""
		Green   = ""
		Yellow  = ""
		Blue    = ""
		Purple  = ""
		Cyan    = ""
		Gray    = ""
		White   = ""
	}
}

Invoke like this:

func main() {
    fmt.Println(color.White + "This is White" + color.Reset)
    fmt.Println(color.Red + "This is Red" + color.Reset)
    fmt.Println(color.Green + "This is Green" + color.Reset)
    fmt.Println(color.Yellow + "This is Yellow" + color.Reset)
    fmt.Println(color.Blue + "This is Blue" + color.Reset)
    fmt.Println(color.Purple + "This is Purple" + color.Reset)
    fmt.Println(color.Cyan + "This is Cyan" + color.Reset)
    fmt.Println(color.Gray + "This is Gray" + color.Reset)
}

 

Go fatih/color Package docs are HERE


 

Simon Tony wrote a Medium article Terminal color render by Golang