V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
pennai
V2EX  ›  问与答

请教一个 go 的编码风格问题

  •  
  •   pennai · 2022-06-25 16:06:10 +08:00 · 780 次点击
    这是一个创建于 663 天前的主题,其中的信息可能已经有所发展或是发生改变。

    下面是 rpc 包下的 client.go 里 Call 方法源码,这里把 reply 作为方法参数传入,返回值是 error 。
    go 可以有多返回值,将 reply 与 error 一起作为返回值的风格,与将 reply 作为参数传入的风格(即下面源码的风格),孰优孰劣呢?

    // Call invokes the named function, waits for it to complete, and returns its error status.
    func (client *Client) Call(serviceMethod string, args any, reply any) error {
    	call := <-client.Go(serviceMethod, args, reply, make(chan *Call, 1)).Done
    	return call.Error
    }
    
    2 条回复    2022-06-26 00:56:16 +08:00
    SimonDing
        1
    SimonDing  
       2022-06-25 16:17:51 +08:00   ❤️ 1
    reply 作为参数,只是想知道 reply 的类型吧。之前 go 没有泛型,函数想知道返回的类型只能这么搞
    pennai
        2
    pennai  
    OP
       2022-06-26 00:56:16 +08:00
    @SimonDing 明白了,谢谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1100 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 23:00 · PVG 07:00 · LAX 16:00 · JFK 19:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.