Colored text dos't work on windows

i have this code but only works on Linux, what i have to do so it can work in Windows, Linux, Mac ect…

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!"))
}
1 Like

It won’t. It’s a feature of Linux (and possibly Mac, I’m not sure), but not windows. Windows do have ways to do colors, but it’s a simpler system than on Linux.

Do you have any example? or where can i look for that?

Windows why is much more complex than linux’ as windows needs some function calls that sideway the character stream. Including explicit flushes every couple of bytes to make sure the new colour will be applied from the right characters.

Though as far as I know, modern windows terminals (somewhen introduced in win10) are capable of understanding ANSI codes for colouring.

Perhaps it’s also Powershell only? Nor exactly sure…

I was hoping that there was and easy way for all OS

There might be libraries which wrap the system in a way that you can just use their API.

Though I never have used on in go so far.

Last time was for ruby, and it didn’t work well. The API was not as clumsy as the windows one, but not as straight forward as the Linux one. And it flushes the stream after every character… It was not a nice experience…

Usually it’s the easiest to just not use color when on windows.

There is a library that demonstrates how to achieve this on Windows. It basically comes down to making a system call to tell the os how to behave.

The actual magic is in

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.