Go中结构自身的方法
Method for struct itself in Go
我注意到在 ORM 库 Gorm 中,您可以使用如下结构定义自定义 table 名称:
type DeviceStatus struct {
// Define some Gorm model fields here
}
// TableName custumizes DeviceStatus SQL table name
func (DeviceStatus) TableName() string {
return "CustomDeviceStatusTableName"
}
这个结构叫什么?我试图用 Google 找到它并弄清楚如何进行函数调用。
对,它实现了接口,如果 TableName() 为空,gorm 将从 TableName() 方法和结构名称中获取 table 名称。
您正在实现 tabler
接口。它只包含一个方法:
type tabler interface {
TableName() string
}
阅读规范中有关 Interface types 的更多信息。
我注意到在 ORM 库 Gorm 中,您可以使用如下结构定义自定义 table 名称:
type DeviceStatus struct {
// Define some Gorm model fields here
}
// TableName custumizes DeviceStatus SQL table name
func (DeviceStatus) TableName() string {
return "CustomDeviceStatusTableName"
}
这个结构叫什么?我试图用 Google 找到它并弄清楚如何进行函数调用。
对,它实现了接口,如果 TableName() 为空,gorm 将从 TableName() 方法和结构名称中获取 table 名称。
您正在实现 tabler
接口。它只包含一个方法:
type tabler interface {
TableName() string
}
阅读规范中有关 Interface types 的更多信息。