go - 如何在更改字符串方法的类型之前打印类型
问题描述
在更改类型的 String 方法中具有 String 方法的字符串方法之前,我无法在类型上调用 Print:
type book struct{
id int
r relateF
//Or can be delare as r relateF
}
type relateF struct{
handle int
grade float64
name string
}
func(b book) String() string{
fmt.Println(b)//<=I want to print it before change String method
return fmt.Sprintf(b.r.name)
}
func main(){
b1:= book{456,relateF{5,48.2,"History of the world"}}
fmt.Println(b1)
}
它做了一个循环
解决方案
一种方法是声明一个具有完全相同结构 a 的新临时类型,book
然后将book
实例转换为这种新类型。
func (b book) String() string {
type temp book // same structure but no methods
fmt.Println(temp(b)) // convert book to temp
return fmt.Sprintf(b.r.name)
}
推荐阅读
- python - 在 Python 中将文本添加到文件的末尾,但不创建新行
- python - Countif pandas python用于带有通配符的多列
- python - jedi-vim 无法初始化 python
- sql-server - Dapper 查询是否在 QuerySingle 一笔交易中执行?
- sqlite - 当第一个选择查询返回 0 时从第二个选择查询获取结果
- python - 如何让 python 浏览网站并获取有用的数据?
- javascript - 使用 CSS 滚动行为时如何从 URL 中删除#id
- c++ - _wmemcpy 替换 64 位程序
- node.js - 从另一个控制器向控制器发布数据
- reactjs - 在 react-native 中使用导航发送参数