V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
pennai
V2EX  ›  问与答

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

  •  
  •   pennai · Jun 25, 2022 · 1157 views
    This topic created in 1401 days ago, the information mentioned may be changed or developed.

    下面是 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 replies    2022-06-26 00:56:16 +08:00
    SimonDing
        1
    SimonDing  
       Jun 25, 2022   ❤️ 1
    reply 作为参数,只是想知道 reply 的类型吧。之前 go 没有泛型,函数想知道返回的类型只能这么搞
    pennai
        2
    pennai  
    OP
       Jun 26, 2022
    @SimonDing 明白了,谢谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1273 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 23:58 · PVG 07:58 · LAX 16:58 · JFK 19:58
    ♥ Do have faith in what you're doing.