handler.ServeHTTP(w,req) and handler(w,req) difference in tests

58 Views Asked by At

I'm trying to understand the difference between handler.ServeHTTP(w,req) and handler(w,req) in Go testing. When should I use each of them? Are they exactly the same?

Docs say simply: ServeHTTP calls f(w, r).

for _, tt := range tests {
    t.Run(tt.name, func(t *testing.T) {
        request := httptest.NewRequest(http.MethodGet, "/status", nil)
        w := httptest.NewRecorder()
        h := http.HandlerFunc(handlers.StatusHandler)
      
        h.ServeHTTP(w, request)
        /// or should it be
        h(w, request)
        
        ...
    }
}
1

There are 1 best solutions below

0
On

As @peter and @mkopriva pointed out, the options are exactly the same and it's a matter of taste