Golang 格式化对象 String()

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 引用 像其他语言都有对应的函数可以打印对象的描述 比如Java中的String(),Python中__str__() Golang 中是否有对应的用法呢 答案是有的 说明 在Golang中,类只要实现了String(),在执行format时,就会自动调用这个方法。 If an operand implements method String() string, that method >will be invoked to convert the object to a string, which will then >be formatted as required by the verb (if any). For compound operands such as slices and structs, the format applies to the elements of each operand, recursively, not to the operand as a whole. Thus %q will quote each element of a slice of strings, and %6.2f will control formatting for each element of a floating-point array. ...

January 26, 2018 · 1 min