Go语言:控制台输出彩色字符
控制台&终端输出打印彩色字符
Golang中的颜色对应序列:
前景色 背景色
30 40 黑色
31 41 红色
32 42 绿色
33 43 黄色
34 44 蓝色
35 45 紫色
36 46 青色
37 47 白色
直接使用Sprintf格式化字符并返回,示例:
fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", color, str)
%d
输出标准的十进制格式化,这里对应颜色表序列。%s
表示输出的字符串
代码示例:
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)
}
调用输出:
fmt.Println(White("test"))
到此这篇关于“Go语言:控制台输出彩色字符”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!
您可能感兴趣的文章:
Go语言:控制台输出彩色字符
Go语言发展历史、核心、特性及学习路线
Go 语言十年而立,Go2 蓄势待发
从零开始学习GO语言-搭建Go语言开发环境-快速开发入门第一个小程序
Go 语言结构
Go语言爱好者周刊:第 78 期 — 这道关于 goroutine 的题
Go语言学习3----Go语言特色
Go 语言学习第一章节
Go 语言 for 循环
go编程输入函数