Skip to content

Instantly share code, notes, and snippets.

@ik5
Last active April 8, 2024 14:25
Show Gist options
  • Save ik5/d8ecde700972d4378d87 to your computer and use it in GitHub Desktop.
Save ik5/d8ecde700972d4378d87 to your computer and use it in GitHub Desktop.
Simple golang expirement with ANSI colors
package main
// http://play.golang.org/p/jZ5pa944O1 <- will not display the colors
import "fmt"
const (
InfoColor = "\033[1;34m%s\033[0m"
NoticeColor = "\033[1;36m%s\033[0m"
WarningColor = "\033[1;33m%s\033[0m"
ErrorColor = "\033[1;31m%s\033[0m"
DebugColor = "\033[0;36m%s\033[0m"
)
func main() {
fmt.Printf(InfoColor, "Info")
fmt.Println("")
fmt.Printf(NoticeColor, "Notice")
fmt.Println("")
fmt.Printf(WarningColor, "Warning")
fmt.Println("")
fmt.Printf(ErrorColor, "Error")
fmt.Println("")
fmt.Printf(DebugColor, "Debug")
fmt.Println("")
}
@PoseidonCoder
Copy link

PoseidonCoder commented Dec 19, 2020

None of this seems to work for me. (I'm on a Windows 10 computer using go1.15.5 windows/amd64)

@holicc
Copy link

holicc commented Dec 21, 2020

None of this seems to work for me. (I'm on a Windows 10 computer using go1.15.5 windows/amd64)

try on win10 terminal ?

@ik5
Copy link
Author

ik5 commented Dec 21, 2020

the following link writes that in order to enable colors in Windows terminal you need to change the registry a bit:

HKEY_CURRENT_USER\Console\VirtualTerminalLevel
"VirtualTerminalLevel"=dword:00000001

@Yariya
Copy link

Yariya commented Mar 10, 2021

Rainbow Console Text:

package main

import (
	"fmt"
	"math/rand"
	"os"
	"time"
)

func rainbow(i string, timeout int) {
	colors := []string{
		"\033[;31m",
		"\033[;34m",
		"\033[;32m",
		"\033[;35m",
		//Add more if needed
	}
	for {
		n := rand.Int() % len(colors)
		fmt.Printf("\r%s%s", colors[n], i)
		os.Stdout.Sync()
		time.Sleep(time.Millisecond * time.Duration(timeout))
	}

}
func main() {
	rainbow("Text", 300) // Text | Timeout
}

@G2G2G2G
Copy link

G2G2G2G commented Oct 18, 2021

@zaydek-old in case you're colorblind and didn't know, your colors are wrong. See the original post for correct colors.

@andyfeller
Copy link

❤️

@DennisPing
Copy link

DennisPing commented Apr 11, 2022

Thank you. Very helpful for go test. Anything that fails prints Red so I can easily spot it. (on Linux)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment