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 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型