咨询 golang 接口导入问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
chaleaochexist
V2EX    Go 编程语言

咨询 golang 接口导入问题

  •  
  •   chaleaochexist 58 天前 1884 次点击
    这是一个创建于 58 天前的主题,其中的信息可能已经有所发展或是发生改变。

    调用方定义一个接口, 这个接口定义了一个方法, 方法的返回值是一个接口. 因为返回的这个接口本身也是一个抽象类型. 这样设计是否合理?

    具体案例可以参考 https://github.com/chaleaoch/golang_demo/tree/master/v5 我得问题也在代码的注释中, 请搜索关键字 "问题"

    这个返回接口的方法是: https://github.com/chaleaoch/golang_demo/blob/master/v5/internal/task/a.go#L20

    15 条回复    2025-08-15 17:43:12 +08:00
    3img
        1
    3img  
       58 天前
    在 Go 里,接口的实现是在 编译期 确定的,而不是像 Python 、Javascript 那样运行时动态绑定。
    sthwrong
        2
    sthwrong  
       58 天前
    返回值可以是接口类型,主要看你的需求,你如果同时依赖实例的其他方法,返回的接口也要声明该方法。
    sthwrong
        3
    sthwrong  
       58 天前
    直接返回实例,即使接口没有声明该方法,也可以被调用。
    sthwrong
        4
    sthwrong  
       58 天前
    @sthwrong #2 一定要调用的话,可以用断言判断能否转成对应实例,再调用
    sthwrong
        5
    sthwrong  
       58 天前   1
    cryptovae
        6
    cryptovae  
       58 天前
    不要为了接口而写接口,接口的本质是归类,将一些 struct 的共同方法抽离出来,如果你本身只有一个 struct 且封装了一个对应的接口,这简直是脱裤子放屁
    Curtion
        7
    Curtion  
       58 天前
    一般情况下接收接口,返回结构体,相关内容的关键词是: Accept interfaces, return structs ,当然这个观点也有争议,不过可以作为一个切入点看看其它人是怎么想的。
    chaleaochexist
        8
    chaleaochexist  
    OP
       58 天前
    @sthwrong 你是天才! 谢谢.
    mcfog
        9
    mcfog  
       58 天前
    这个做法本身逻辑、风格等方面都没有问题,但有一个点是为了避免类型耦合,被返回的接口最好是 alias of unnamed type

    以 OP 代码为例,本来没有耦合的结构因为 SSHClient 是 named type ,导致实现方需要依赖调用方(的类型)才能实现接口,白费了这一层抽象。修改方法是

    - type SSHClient interface {
    + type SSHClient = interface {

    然后这个 type 在实现侧拷贝一份。Golang 格言:A little copying is better than a little dependency.
    rainbowhu
        10
    rainbowhu  
       58 天前
    provider 接口和 client 接口共同组成了一套对外的接口,没觉得有啥问题。
    chaleaochexist
        11
    chaleaochexist  
    OP
       58 天前
    @cryptovae 主要是为了单元测试.

    @mcfog 哦!!! soga 我试试, 从来没见过还可以这么写. 受教了!!!
    spritecn
        12
    spritecn  
       57 天前
    @sthwrong 最近看字节的示例好多都是直接不用接口,直接用函数类型 ,然后断言调用,其实蛮好的,很多接口其实只有一个函数
    chaleaochexist
        13
    chaleaochexist  
    OP
       57 天前
    @spritecn 举个例子佬.
    mb4555
        14
    mb4555  
       57 天前
    @spritecn 那什么要用接口
    spritecn
        15
    spritecn  
       57 天前   1
    @chaleaochexist
    // Copyright 2022 CloudWeGo Authors
    //
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.

    package netpoll

    import (
    "net"
    "time"
    )

    // CloseCallback will be called after the connection is closed.
    // Return: error is unused which will be ignored directly.
    type CloseCallback func(connection Connection) error
    ---
    netpoll 里随处可见
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2737 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 11:51 PVG 19:51 LAX 04:51 JFK 07:51
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86