duanmu0834 2016-07-18 04:38
浏览 38
已采纳

如果os / exec包中的Go类型ExitError不在文档中,则如何支持Sys()方法?

Based on various examples on the web and in the answer to this SO question, I am trying to figure out how it is possible for type ExitError from package os/exec to support the Sys() method even if the docs only mention the Error() method for this type.

I've guessed that the Sys() method in question is from type ProcessState in package os, but how does ExitError get to use it directly (exiterror.Sys()) without having to use the full (exiterror.ProcessState.Sys())?

This must be a basic Go question but I've yet to figure out the answer to this one one my own...

  • 写回答

1条回答 默认 最新

  • doulai2025 2016-07-18 04:51
    关注

    cmd.Wait() already returns error of type *ExitError. If you look at ExitError's definition, you can see that it embeds *os.ProcessState:

    type ExitError struct {
            *os.ProcessState
            // other fields
    }
    

    It is through *os.ProcessState that a value of type ExitError can call Sys() method.

    Note that within the definition of ExitError, there is no field name associated with *os.ProcessState, which means that a value of type ExitError can directly call any method on *os.ProcessState (sort of like inheritance, where ExitError inherits *os.ProcessState. But this is only to give you a very basic idea. Read the docs for clarification.) as long as there is no method defined on ExitError with the same name.

    There is of course more to it. You can read about it here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器