V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Coding.NET 轻量级社交
开源项目广场
使用帮助
意见反馈
hanfengsmile
V2EX  ›  Coding

go struct 包含 interface

  •  
  •   hanfengsmile · 2018-02-26 17:25:54 +08:00 · 3223 次点击
    这是一个创建于 2222 天前的主题,其中的信息可能已经有所发展或是发生改变。

    type Interface interface { Len() int Less(i, j int) bool Swap(i, j int) }

    type reverse struct { Interface }

    这段代码有大神可以解释一下么,看不懂

    3 条回复    2018-02-26 18:13:03 +08:00
    zhujinliang
        1
    zhujinliang  
       2018-02-26 17:34:54 +08:00 via iPhone   ❤️ 1
    跟粘合一个结构体一样,相当于有个名叫 Interface 的字段,类型是 Interface
    rrfeng
        2
    rrfeng  
       2018-02-26 17:39:08 +08:00   ❤️ 1
    type A struct{}

    type B struct { A }

    一样一样的
    hanfengsmile
        3
    hanfengsmile  
    OP
       2018-02-26 18:13:03 +08:00   ❤️ 1
    @zhujinliang
    @rrfeng

    那这样写的好处是什么呢,看了 stackoverfloe 一个回答,没有理解

    https://stackoverflow.com/questions/24537443/meaning-of-a-struct-with-embedded-anonymous-interface
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1065 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 22:45 · PVG 06:45 · LAX 15:45 · JFK 18:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.