Coding.NET 轻量级社交
开源项目广场
使用帮助
意见反馈
hanfengsmile
V2EX  ›  Coding

go struct 包含 interface

  •  
  •   hanfengsmile · Feb 26, 2018 · 4093 views
    This topic created in 3027 days ago, the information mentioned may be changed or developed.

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

    type reverse struct { Interface }

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

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

    type B struct { A }

    一样一样的
    hanfengsmile
        3
    hanfengsmile  
    OP
       Feb 26, 2018   ❤️ 1
    @zhujinliang
    @rrfeng

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

    https://stackoverflow.com/questions/24537443/meaning-of-a-struct-with-embedded-anonymous-interface
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3234 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 11:04 · PVG 19:04 · LAX 04:04 · JFK 07:04
    ♥ Do have faith in what you're doing.