ansi Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/ansi/ Fri, 08 Jan 2021 16:26:36 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 ansi Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/ansi/ 32 32 159787465 Print/Output text in color in console https://vikasboss.github.io/print-output-text-color-console/ https://vikasboss.github.io/print-output-text-color-console/#respond Sun, 29 Mar 2020 19:24:16 +0000 https://vikasboss.github.io/?p=1902 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....

The post Print/Output text in color in console appeared first on Welcome To Golang By Example.

]]>
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

The post Print/Output text in color in console appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/print-output-text-color-console/feed/ 0 1902