dpstir0081 2018-11-24 08:03
浏览 250
已采纳

Golang返回接口

Let say we want to extend Error() function on error interface. We can simply create a struct derived from string implementing the Error() method. For example:

type NewUser struct {
    Email           string
    Password        string
}
type ErrMissingField string

func (e ErrMissingField) Error() string {
    return string(e) + " is required"
}

func (u *NewUser) OK() error {
    if len(u.Email) == 0 {
        return ErrMissingField("email")
    }
    if len(u.Password) == 0 {
        return ErrMissingField("password")
    }
    return nil
}

Above code will return either email is required or password is required.

But, if I create my own interface, let say ResponseError, like this:

type ResponseError interface {
    ErrorMsg() string
}

type CustomErr string

func (c CustomErr) ErrorMsg() string {
    return "[Error] " + string(c)
}
func (u *NewUser) NewOK() ResponseError {
    if len(u.Email) == 0 {
        return CustomErr("Email required!")
    }
    if len(u.Password) == 0 {
        return CustomErr("Password Required!")
    }
    return nil
}

It won't print the method implementation I wrote with [Error]. It just prints the string I passed into the struct Email required! or Password Required!.

How to work on this?

  • 写回答

2条回答 默认 最新

  • dse84825 2018-11-24 08:30
    关注

    If you are using fmt to print, then from https://golang.org/pkg/fmt

    1. If an operand implements the error interface, the Error method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).

    2. If an operand implements method String() string, that method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any)

    That's why when you implement Error interface, it prints by invoking Error() function. If you want custom output for other interface like ResponseError implement String() method.

    package main
    
    import (
        "fmt"
    )
    
    type NewUser struct {
        Email           string
        Password        string
    }
    
    type ResponseError interface {
        ErrMsg()
        String() string
    }
    
    type CustomErr string
    
    func (c CustomErr) String() string {
        return "[Error] " + string(c)
    }
    
    func (c CustomErr) ErrMsg() {}
    
    func (u *NewUser) NewOK() ResponseError {
        if len(u.Email) == 0 {
            return CustomErr("Email required!")
        }
        if len(u.Password) == 0 {
            return CustomErr("Password Required!")
        }
        return nil
    }
    
    func main() {
        u := &NewUser{}
        fmt.Println(u.NewOK())
    }
    

    Go playground: https://play.golang.org/p/khAAtLodEND

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏