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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀