duanjianxu4288 2019-09-10 22:21
浏览 41

返回&obj是什么意思? 它对平等检查有什么影响

What is the difference in returning

func New(text string) error { return &errorString{text} }

or returning like

func New(text string) error { return errorString{text} }

errorString is defined as follow

type errorString struct { text string }

error is defined as follow

type error interface {
  Error() string
}

I especially want to know what the difference in the return value is:

return &errorString{text}
      vs.
return errorString{text}

I already read the guide but it didn't mentioned the difference. It only mentioned, that with one you are not able to use equal what you do not want with the error object.

  • 写回答

1条回答 默认 最新

  • dongzong7467 2019-09-10 22:36
    关注

    errorString{text} is a struct and &errorString{text} is a pointer to a struct. See https://tour.golang.org/moretypes/1 for a more detailed explanation of what this means.

    There is also this question which has specifics on when to return which: Pointers vs. values in parameters and return values. Returning errorText{text} returns a copy of the entire struct while &errorText{text} returns a pointer to a struct instead.

    A struct S and a pointer to a struct of type S are two separate types in Go. For errorText to implement the error interface, errorText must implement the Error() string method but it also needs to have the correct receiver type.

    For example, this is OK:

    type errorText struct {
        text string
    }
    
    func (e errorText) Error() string { return e.text }
    
    func SomethingThatReturnsYourError() error {
        return errorText{"an error"}
    }
    

    This is also OK:

    type errorText struct {
        text string
    }
    
    func (e errorText) Error() string { return e.text }
    
    func SomethingThatReturnsYourError() error {
        return &errorText{"an error"} // Notice the &
    }
    

    But this is not OK and the compiler will hit you with an error:

    type errorText struct {
        text string
    }
    
    // notice the pointer receiver type
    func (e *errorText) Error() string { return e.text }
    
    func SomethingThatReturnsYourError() error {
        return errorText{"an error"}
    }
    

    Error message: cannot use errorString literal (type errorString) as type error in return argument: errorString does not implement error (Error method has pointer receiver)

    In order to use a pointer receiver you have to return a pointer:

    type errorText struct {
        text string
    }
    
    func (e *errorText) Error() string { return e.text }
    
    func SomethingThatReturnsYourError() error {
        return &errorText{"an error"}
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题