os.Error - string value (Golang)

后端 未结 2 1143

How do I get the string value of os.Error? ie. assign to a variable.

2条回答
  •  礼貌的吻别
    2021-02-11 15:50

    Update based on go1 release notes:

    Use err.Error() to get the string value.

    Example:

    package main
    
    import (
        "fmt"
        "errors"
        "runtime"
    )
    
    func main() {
        err := errors.New("use of err.String() detected!")
        s := err.Error()
        fmt.Printf(
           "version: %s\ntypes: %T / %T\nstring value via err.Error(): %q\n",
           runtime.Version(), err, s, s)
    }
    

    output:

    go run main102.go
    version: go1.0.2
    types: *errors.errorString / string
    string value via err.Error(): "use of err.String() detected!"
    

提交回复
热议问题