0% found this document useful (0 votes)
8 views11 pages

Go http.Handler Interface Explained

The document discusses the http.Handler interface in Go and how functions can be adapted to serve as handlers. It explains that any type with a ServeHTTP method taking a ResponseWriter and request pointer can be a handler. It also demonstrates how the HandlerFunc adapter allows ordinary functions to serve as handlers by implementing ServeHTTP for them.

Uploaded by

Aiden
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

Go http.Handler Interface Explained

The document discusses the http.Handler interface in Go and how functions can be adapted to serve as handlers. It explains that any type with a ServeHTTP method taking a ResponseWriter and request pointer can be a handler. It also demonstrates how the HandlerFunc adapter allows ordinary functions to serve as handlers by implementing ServeHTTP for them.

Uploaded by

Aiden
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

8/14/23, 10:12 AM Understanding http.

Handler interface — Adaptor Function in Go | by Chandan Jhunjhunwal | Aug, 2023 | Medium

Understanding [Link] interface —


Adaptor Function in Go
Chandan Jhunjhunwal · Follow
2 min read · 4 days ago

Listen Share

I was trying to understand the Handler interface in http standard library. In case
you don’t know interfaces in Go, you can read a very well explained blog article here
by Alex Edward.

If you look at the go http package and the source code here

type Handler interface {


ServeHTTP(ResponseWriter, *Request)
}

[Link] is an interface , which has a method ServeHTTP . Which means, any


type, having method ServeHTTP with ResponseWriter interface and a pointer to
Request can be treated a [Link] .

Let’s see this with example:

[Link] 1/11
8/14/23, 10:12 AM Understanding [Link] interface — Adaptor Function in Go | by Chandan Jhunjhunwal | Aug, 2023 | Medium

type chandan struct {}

func (h *chandan) ServeHTTP(w [Link], r *[Link]) {


[Link]([]byte("This is my home page"))
}

How we can serve this page with following:

mux := [Link]()
[Link]("/", &chandan{}

Now, instead of writing this, we can have a simpler approach.

func chandan(w [Link], r *[Link]) {


[Link]([]byte("This is my home page"))
}

And now, this can be served with HandlerFunc adaptor.

mux := [Link]()
[Link]("/", [Link](chandan))

How how does this [Link](home) works?

Let’s try to understand this with an example:

package main

import "fmt"

type HandlerFunc func(int, int) int

func (f HandlerFunc) MyFunction(num1 int, num2 int) int {


[Link] 2/11
8/14/23, 10:12 AM Understanding [Link] interface — Adaptor Function in Go | by Chandan Jhunjhunwal | Aug, 2023 | Medium

return f(num1, num2)


}

func home(x, y int) int {


return x + y
}

func main() {
handler := HandlerFunc(home)
result := [Link](10, 20) // Notice, MyFunction is callable on `hom
[Link](result)
}

If you notice, I defined a HandlerFunc function type and defined a method


MyFunction , which takes two integers as arguments and returns an integer.

Now I’m able to call the function defined on HandlerFunc with home . This is how
many of the features in Go standard libraries are implemented to leverage it’s
potential.
Open in app Sign up Sign In

Now back to our original question “How how does this [Link](home)

works?”

HandlerFunc function type has a method defined as ServeHTTP .

// The HandlerFunc type is an adapter to allow the use of


// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)

// ServeHTTP calls f(w, r).


func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
f(w, r)
}

Following our above example, we can clearly see, that the HandlerFunc adaptor adds
a method ServeHTTP on our original function chandan .

[Link] 3/11
8/14/23, 10:12 AM Understanding [Link] interface — Adaptor Function in Go | by Chandan Jhunjhunwal | Aug, 2023 | Medium

mux := [Link]()
// [Link]("/", [Link](chandan)) // This can be simplified by foll
[Link]("/", home)

Chaining Handlers:

The ServeMux is a special type of Handler , which passes the request to second
handler. That’s why, we can pass mux as second argument to ListenAndServe .

func ListenAndServe(addr string, handler Handler) error

Go Golang Golang Tutorial

Follow

Written by Chandan Jhunjhunwal


109 Followers

More from Chandan Jhunjhunwal

[Link] 4/11
8/14/23, 10:12 AM Understanding [Link] interface — Adaptor Function in Go | by Chandan Jhunjhunwal | Aug, 2023 | Medium

Chandan Jhunjhunwal

All URL with domain [Link] times out


You landed on this page, probably because you faced following error:

2 min read · Jun 8

Chandan Jhunjhunwal in Faodail Technology

Rails API Authentication using JWT

[Link] 5/11
8/14/23, 10:12 AM Understanding [Link] interface — Adaptor Function in Go | by Chandan Jhunjhunwal | Aug, 2023 | Medium

The conventional way of API authentication is via a secret token. Though this serves the
purpose, but JWT adds a lot of flexibility and…

2 min read · Mar 17, 2017

94 2

Chandan Jhunjhunwal

LoadError: cannot load such file — 2.6/ffi_c


While rails db:create — Following command fixes the i

1 min read · Feb 26, 2022

[Link] 6/11
8/14/23, 10:12 AM Understanding [Link] interface — Adaptor Function in Go | by Chandan Jhunjhunwal | Aug, 2023 | Medium

Chandan Jhunjhunwal in Faodail Technology

Step by step guide to integrating DynamoDB with Ruby on Rails


Application — I
A typical Ruby on Rails application comes with ActiveRecord, which makes using RDBMS (such
as MySQL, PostgreSQL etc) cake walk. There are…

4 min read · May 19, 2017

30 2

See all from Chandan Jhunjhunwal

Recommended from Medium

[Link] 7/11
8/14/23, 10:12 AM Understanding [Link] interface — Adaptor Function in Go | by Chandan Jhunjhunwal | Aug, 2023 | Medium

Abhinav in Stackademic

Securing Go Applications: Best Practices and Libraries to Defend Against


Vulnerabilities
Security is paramount when developing applications, and Go provides various tools and
practices to help developers protect their…

· 3 min read · Aug 6

Matthias Bruns

[Link] 8/11
8/14/23, 10:12 AM Understanding [Link] interface — Adaptor Function in Go | by Chandan Jhunjhunwal | Aug, 2023 | Medium

Golang — The Ultimate Guide to Dependency Injection


Comparing manual and framework Dependency Injection

· 17 min read · Mar 2

156 2

Lists

General Coding Knowledge


20 stories · 197 saves

Now in AI: Handpicked by Better Programming


262 stories · 81 saves

Nelson Parente in Better Programming

Google’s Wire: Automated Dependency Injection in Go


Building flexible and maintainable Go applications

· 5 min read · Mar 3

198 5

[Link] 9/11
8/14/23, 10:12 AM Understanding [Link] interface — Adaptor Function in Go | by Chandan Jhunjhunwal | Aug, 2023 | Medium

Igor Carvalho

Go lang: From 0 to Employed


S.O.L.I.D principles :: Introduction

9 min read · Jun 6

11

Abhinav

Building a Telegram Bot and Integrating it with a Golang: A Step-by-Step


Guide
[Link] 10/11
8/14/23, 10:12 AM Understanding [Link] interface — Adaptor Function in Go | by Chandan Jhunjhunwal | Aug, 2023 | Medium

Telegram is a popular messaging platform that allows users to create and interact with bots. In
this tutorial, we’ll walk you through the…

· 3 min read · Aug 6

Khaled Karam in The Qonto Way

Transactions in Go Hexagonal Architecture


Qonto has hundreds of microservices collaborating with each other to deliver our product’s
features to our customers. To keep each…

6 min read · Aug 3

103

See more recommendations

[Link] 11/11

You might also like