cannot convert data (type interface {}) to type string: need type assertion

前端 未结 4 726
予麋鹿
予麋鹿 2020-12-07 07:28

I am pretty new to go and I was playing with this notify package.

At first I had code that looked like this:

func doit(w http.ResponseWriter, r *http         


        
相关标签:
4条回答
  • 2020-12-07 07:45
    //an easy way:
    str := fmt.Sprint(data)
    
    0 讨论(0)
  • 2020-12-07 07:50

    Type Assertion

    This is known as type assertion in golang, and it is a common practice.

    Here is the explanation from a tour of go:

    A type assertion provides access to an interface value's underlying concrete value.

    t := i.(T)
    

    This statement asserts that the interface value i holds the concrete type T and assigns the underlying T value to the variable t.

    If i does not hold a T, the statement will trigger a panic.

    To test whether an interface value holds a specific type, a type assertion can return two values: the underlying value and a boolean value that reports whether the assertion succeeded.

    t, ok := i.(T)
    

    If i holds a T, then t will be the underlying value and ok will be true.

    If not, ok will be false and t will be the zero value of type T, and no panic occurs.

    NOTE: value i should be interface type.

    Pitfalls

    Even if i is an interface type, []i is not interface type. As a result, in order to convert []i to its value type, we have to do it individually:

    // var items []i
    for _, item := range items {
        value, ok := item.(T)
        dosomethingWith(value)
    }
    

    Performance

    As for performance, it can be slower than direct access to the actual value as show in this stackoverflow answer.

    0 讨论(0)
  • 2020-12-07 07:58

    According to the Go specification:

    For an expression x of interface type and a type T, the primary expression x.(T) asserts that x is not nil and that the value stored in x is of type T.

    A "type assertion" allows you to declare an interface value contains a certain concrete type or that its concrete type satisfies another interface.

    In your example, you were asserting data (type interface{}) has the concrete type string. If you are wrong, the program will panic at runtime. You do not need to worry about efficiency, checking just requires comparing two pointer values.

    If you were unsure if it was a string or not, you could test using the two return syntax.

    str, ok := data.(string)
    

    If data is not a string, ok will be false. It is then common to wrap such a statement into an if statement like so:

    if str, ok := data.(string); ok {
        /* act on str */
    } else {
        /* not string */
    }
    
    0 讨论(0)
  • 2020-12-07 07:58

    As asked for by @ρяσѕρєя an explanation can be found at https://golang.org/pkg/fmt/#Sprint. Related explanations can be found at https://stackoverflow.com/a/44027953/12817546 and at https://stackoverflow.com/a/42302709/12817546. Here is @Yuanbo's answer in full.

    package main
    
    import "fmt"
    
    func main() {
        var data interface{} = 2
        str := fmt.Sprint(data)
        fmt.Println(str)
    }
    
    0 讨论(0)
提交回复
热议问题