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

go struct 包含 interface

  •  
  •   hanfengsmile · Feb 26, 2018 · 4365 views
    This topic created in 3188 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
    zhujinliang
    Feb 26, 2018 via iPhone❤️1
    1
    跟粘合一个结构体一样,相当于有个名叫 Interface 的字段,类型是 Interface
    rrfeng
    rrfeng
    Feb 26, 2018❤️1
    2
    type A struct{}

    type B struct { A }

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

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

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