Skip to content

Instantly share code, notes, and snippets.

@ik5
Last active Sep 8, 2021
Embed
What would you like to do?
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("")
}
@maxmcd

This comment has been minimized.

Copy link

@maxmcd maxmcd commented Mar 13, 2019

Also:

package main

import "fmt"

const (
	PrintColor = "\033[38;5;%dm%s\033[39;49m\n"
)

func main() {
	for j := 0; j < 256; j++ {
		fmt.Printf(PrintColor, j, "Hello!")
	}
}
@zaydek-old

This comment has been minimized.

Copy link

@zaydek-old zaydek-old commented Nov 5, 2019

I built on this, changed a few small things:

package main

import "fmt"

var (
  Info = Teal
  Warn = Yellow
  Fata = Red
)

var (
  Black   = Color("\033[1;30m%s\033[0m")
  Red     = Color("\033[1;31m%s\033[0m")
  Green   = Color("\033[1;32m%s\033[0m")
  Yellow  = Color("\033[1;33m%s\033[0m")
  Purple  = Color("\033[1;34m%s\033[0m")
  Magenta = Color("\033[1;35m%s\033[0m")
  Teal    = Color("\033[1;36m%s\033[0m")
  White   = Color("\033[1;37m%s\033[0m")
)

func Color(colorString string) func(...interface{}) string {
  sprint := func(args ...interface{}) string {
    return fmt.Sprintf(colorString,
      fmt.Sprint(args...))
  }
  return sprint
}

func main() {
  fmt.Println(Info("hello, world!"))
}

Thanks!

@jaylucas

This comment has been minimized.

Copy link

@jaylucas jaylucas commented Dec 19, 2019

@zaydek this is awesome.

@eddyv73

This comment has been minimized.

Copy link

@eddyv73 eddyv73 commented Apr 1, 2020

<3

@urbanishimwe

This comment has been minimized.

Copy link

@urbanishimwe urbanishimwe commented Apr 18, 2020

🤝

@shudipta

This comment has been minimized.

Copy link

@shudipta shudipta commented Apr 24, 2020

Awesome

@coding-to-music

This comment has been minimized.

Copy link

@coding-to-music coding-to-music commented Nov 30, 2020

Awesome indeed

@holicc

This comment has been minimized.

Copy link

@holicc holicc commented Dec 16, 2020

Awesome

@PoseidonCoder

This comment has been minimized.

Copy link

@PoseidonCoder 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

This comment has been minimized.

Copy link

@holicc 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

This comment has been minimized.

Copy link
Owner Author

@ik5 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

This comment has been minimized.

Copy link

@Yariya 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment