1-测试标准颜色 --github.com_fatih_color测试

1.先读README.md文件

安装 color 库
$ go get github.com/fatih/color

在这里插入图片描述

2.实例测试

标准颜色代码

// Print with default helper functions
color.Cyan("Prints text in cyan.")

// A newline will be appended automatically
color.Blue("Prints %s in blue.", "text")

// These are using the default foreground colors
color.Red("We have red")
color.Magenta("And many others ..")

在这里插入图片描述

本地电脑测试

/main.go

package main

import (
	"github.com/fatih/color"
)

func main() {
	// Print with default helper functions
	color.Cyan("Prints text in cyan.")

	// A newline will be appended automatically
	color.Blue("Prints %s in blue.", "text")

	// These are using the default foreground colors
	color.Red("We have red")
	color.Magenta("And many others ..")
}

在这里插入图片描述

3.代码解读

a.从一条代码开始解读

// 用青色打印内容
color.Cyan("Prints text in cyan.")

// 按住ctrl键,点击 Cyan 函数代码

在这里插入图片描述

b.查看color库中的Cyan 函数代码

// color库中的Cyan 函数代
// 在这里我们传入了字符串,接着将字符串带入函数体,将字符串传给colorPrint函数
// 按住ctrl键,点击 FgCyan 函数代码

func Cyan(format string, a ...interface{}) { colorPrint(format, FgCyan, a...) }

在这里插入图片描述

c.查看参数FgCyan

// 查看代码可知:
1.FgCyan是类型为 Attribute 的常量,值为36

在这里插入图片描述

d.查看 Attribute 是什么

// 查看代码可知:
1.Attribute 类型可以被定义为 int 的别名

在这里插入图片描述

f.查看函数 colorPrint

// 由上面代码可知:
1.Cyan 函数将字符串传给 colorPrint 函数
2.colorPrint 函数的第二参数带上常量FgCyan(值为36),第一参数是前面传入的字符串

在这里插入图片描述

colorPrint函数
1.获取p对应的颜色结构体Color
2.如果输出的字符没有换行符,就加上换行符
3.如果不是格式化,就执行 c.Print(format) ,否则执行 c.Printf(format, a...)

在这里插入图片描述

g.颜色打印函数

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桃园码工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值