dongwuwan5646 2014-10-23 11:08
浏览 17
已采纳

无效的接收器类型[] T([] T是未命名的类型)解决方法?

I would like to define a method on []T, where T is a type I defined.
It looks like i have to define a new type to do this, but that stops me from using all the builtin functions for slices on this new type (such as len).

Is the go way of doing this to just make ordinary functions, rather than methods? (Kinda like how append() could be a method, but isn't?)

  • 写回答

1条回答 默认 最新

  • duandi8852752 2014-10-23 11:14
    关注

    You can define a slice type:

    type MySliceType []SomeType
    
    • You can still use append and slicing operations on values of MySliceType.
    • You can define methods on MySliceType.

    You can't, however, monkeypatch []SomeType's methods.

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

报告相同问题?

悬赏问题

  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面