开源包mqtt源码_DEBUG

开源包mqtt源码_DEBUG

巧妙的调试不注释变量

官方代码包

type (
	// Logger interface allows implementations to provide to this package any
	// object that implements the methods defined in it.
	Logger interface {
		Println(v ...interface{})
		Printf(format string, v ...interface{})
	}

	// NOOPLogger implements the logger that does not perform any operation
	// by default. This allows us to efficiently discard the unwanted messages.
	NOOPLogger struct{}
)

func (NOOPLogger) Println(v ...interface{})               {}
func (NOOPLogger) Printf(format string, v ...interface{}) {}

// Internal levels of library output that are initialised to not print
// anything but can be overridden by programmer
var (
	ERROR    Logger = NOOPLogger{}
	CRITICAL Logger = NOOPLogger{}
	WARN     Logger = NOOPLogger{}
	DEBUG    Logger = NOOPLogger{}
)

分析

看这个项目时候总是有这样的代码 DEBUG.Println(CLI, "client is connected/reconnected"), 但是进到源码一看啥都没打印

在自己写项目时候,总是会调试一些东西,有些变量我们会用fmt打印出来,但是不想让变量打印,还要注释上面的var定义,很麻烦,就要用一个空接口来接受我们的需要注释的变量,这个设计很巧妙

果断把他抄作业,带走到我的go-utils工具包里面去

posted @ 2021-08-18 10:55  maob  阅读(129)  评论(0编辑  收藏  举报