This is a Go library for colorizing strings.
To give a color name, write as follows:
package main
import (
"fmt"
"log"
"github.com/ttkzw/go-color"
)
func main() {
c, err := color.NewColor("Green")
if err != nil {
log.Fatal(err)
}
fmt.Println(c.Colorize("OK"))
}
To use built-in constants, write as follows:
package main
import (
"fmt"
"github.com/ttkzw/go-color"
)
func main() {
fmt.Println(color.Green.Colorize("OK"))
}
To use a background color, write as follows:
package main
import (
"fmt"
"github.com/ttkzw/go-color"
)
func main() {
fmt.Println(color.Colorize("OK", color.White, color.GreenBackground))
}
If you want to support Windows, use go-colorable.
package main
import (
"fmt"
"github.com/mattn/go-colorable"
"github.com/ttkzw/go-color"
)
func main() {
var output = colorable.NewColorableStdout()
fmt.Fprintln(output, color.Green.Colorize("OK"))
}
$ go get github.com/ttkzw/go-color
BSD 3-Clause License
Takashi Takizawa