pennai
V2EX  ›  问与答

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

  •  
  •   pennai · Jun 25, 2022 · 1558 views
    This topic created in 1592 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
    SimonDing
    Jun 25, 2022❤️1
    1
    reply 作为参数,只是想知道 reply 的类型吧。之前 go 没有泛型,函数想知道返回的类型只能这么搞
    pennai
    pennai
    OP
    Jun 26, 2022
    2
    @SimonDing 明白了,谢谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1326 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 20:32 · PVG 04:32 · LAX 12:32 · JFK 15:32
    ♥ Do have faith in what you're doing.