给某个类型,定义String() 方法,可以用来自定义输出这个类型被打印时的输出的内容
--------------------------
package main import ( "log" ) type ss map[string]string func (a ss)String() string{ return "zzzzzzz" } func main() { var a ss a = map[string]string{"__name__":"node_scrape_collector_success", "collector":"zfs"} log.Println(len(a)) log.Printf("%v", a) log.Printf("%#v", a) }
上面执行,
root@go:/home/fly/test# go run ll.go
2021/08/13 06:59:28 2
2021/08/13 06:59:28 zzzzzzz
2021/08/13 06:59:28 main.ss{"__name__":"node_scrape_collector_success", "collector":"zfs"}