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

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

At first I had code that looked like this:

  1. func doit(w http.ResponseWriter, r *http.Request) {
  2. notify.Post("my_event", "Hello World!")
  3. fmt.Fprint(w, "+OK")
  4. }

I wanted to append newline to Hello World! but not in the function doit above, because that would be pretty trivial, but in the handler afterwards like this below:

  1. func handler(w http.ResponseWriter, r *http.Request) {
  2. myEventChan := make(chan interface{})
  3. notify.Start("my_event", myEventChan)
  4. data := <-myEventChan
  5. fmt.Fprint(w, data + "\n")
  6. }

After go run:

  1. $ go run lp.go
  2. # command-line-arguments
  3. ./lp.go:15: invalid operation: data + "\n" (mismatched types interface {} and string)

After a little bit of Googling I found this question on SO.

Then I updated my code to:

  1. func handler(w http.ResponseWriter, r *http.Request) {
  2. myEventChan := make(chan interface{})
  3. notify.Start("my_event", myEventChan)
  4. data := <-myEventChan
  5. s:= data.(string) + "\n"
  6. fmt.Fprint(w, s)
  7. }

Is this what I was supposed to do? My compiler errors are gone so I guess that’s pretty good? Is this efficient? Should you do it differently?

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.

  1. 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:

  1. if str, ok := data.(string); ok {
  2. /* act on str */
  3. } else {
  4. /* not string */
  5. }

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.

  1. 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.

  1. 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.


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:

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


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

  1. //an easy way:
  2. str := fmt.Sprint(data)

Add some explanation with answer for how this answer help OP in fixing current issue - ρяσѕρєя K

ft_authoradmin  ft_create_time2019-06-19 11:23
 ft_update_time2019-06-19 11:23