Imprimir caracteres de color en la consola y salida de terminal
La secuencia correspondiente de colores en Golang:
前景色 背景色
30 40 黑色
31 41 红色
32 42 绿色
33 43 黄色
34 44 蓝色
35 45 紫色
36 46 青色
37 47 白色
Utilice directamente Sprintf para formatear caracteres y devolver, por ejemplo:
fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", color, str)
%d
Formato decimal de salida estándar, aquí corresponde a la secuencia de la tabla de colores. %s
Cadena que representa la salida
Ejemplo de código:
const (
textBlack = iota + 30
textRed
textGreen
textYellow
textBlue
textPurple
textCyan
textWhite
)
func Black(str string) string {
return textColor(textBlack, str)
}
func Red(str string) string {
return textColor(textRed, str)
}
func Yellow(str string) string {
return textColor(textYellow, str)
}
func Green(str string) string {
return textColor(textGreen, str)
}
func Cyan(str string) string {
return textColor(textCyan, str)
}
func Blue(str string) string {
return textColor(textBlue, str)
}
func Purple(str string) string {
return textColor(textPurple, str)
}
func White(str string) string {
return textColor(textWhite, str)
}
func textColor(color int, str string) string {
return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", color, str)
}
Salida de llamada:
fmt.Println(White("test"))