'Golang: How to color fmt.Fprintf output?
I know I can add colors to fmt.Println output with something like:
package main
import (
"fmt"
)
func main() {
colorReset := "\033[0m"
colorRed := "\033[31m"
fmt.Println(string(colorRed), "test", string(colorReset))
fmt.Println("next")
}
Is there any way to colorize the output of fmt.Fprintf
?
Solution 1:[1]
In the same way you used the Println you can use colors with Fprintf, ex
const colorRed = "\033[0;31m"
const colorNone = "\033[0m"
func main() {
fmt.Fprintf(os.Stdout, "Red: \033[0;31m %s None: \033[0m %s", "red string", "colorless string")
fmt.Fprintf(os.Stdout, "Red: %s %s None: %s %s", colorRed, "red string", colorNone, "colorless string")
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jhonatan |