-2

I am reading this code and I don't quite understand what line #2 does:

resp := route.Handler(req)
_, nilresponse := resp.(NilResponse)
if !nilresponse {

type NilResponse struct {
}

Thank you

Sergei G
  • 1,561
  • 3
  • 18
  • 26

2 Answers2

1

This isn't an empty function name. This is a type-assertion. It is testing that resp is a NilResponse. If it is, then nilResponse will be true, otherwise it will be false. This code throws away the resulting type-asserted value by using _.

See Type Assertions.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
0

If line two is _, nilresponse := resp.(NilResponse) then it's not a function call at all. It's a type assertion. The code is saying "the interface value represented by resp is of type NilResponse.

EDIT; your assignment is kind of odd though because the first return value would be the NilResponse object and the second (if specified) is a flag to indicate whether or not it worked (or maybe an error, can't remember if it's a bool or error). So typically it would be something like; nilResponse, ok := or nilResponse, err :=

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115