duanhe1903 2019-08-01 15:57
浏览 228
已采纳

接口[golang]中的字段?

is there any solution to force struct have specific property(defined in interface)?
or define a property(attribute,field) in interface?.
as i see interface is always accept method not property?. (https://gobyexample.com/interfaces)

type geo interface {
    PrintType()
    typ string//not function , but field
}
type circle struct {
    typ string
}

func (c circle) PrintType() {
    fmt.Println(c.typ)
}

thanks

  • 写回答

1条回答 默认 最新

  • dsf22567 2019-08-01 16:03
    关注

    Interfaces are about methods only, not properties, because interfaces are about behavior. So there is no way to force a struct to have something. Rather the preferred method is to have an accessor to that member.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬虫保存的scv文件0kb
  • ¥20 如何实现基于强化学习的带电作业机械臂的运动规划与控制
  • ¥15 使用wpf制作打砖块游戏时遇到的一个Bug
  • ¥15 qrCodeDetector.detectAndDecode
  • ¥15 海洋可控源和大地电磁一维联合反演
  • ¥15 MFC多文档程序获取视图指针问题
  • ¥20 解体如何编写程序还有运行结果截图
  • ¥15 如何把matlabR2023遗传算法工具箱里面的各类选项对应的代码调出来呢?
  • ¥30 MDIGetActive() 获取活动view
  • ¥15 有数据,如何安装使用CLIP模型检索图片