0% found this document useful (0 votes)
3 views10 pages

Go Backend

The document provides an overview of Go backend development using the net/http package, highlighting key components such as the server, handlers, and routing. It emphasizes the importance of http.ResponseWriter and *http.Request in handling requests and responses, as well as the evolution of HTTP protocols. Additionally, it discusses the Gin framework as an alternative to net/http, addressing its advantages over verbosity and manual processes.

Uploaded by

harshitbatra0602
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)
3 views10 pages

Go Backend

The document provides an overview of Go backend development using the net/http package, highlighting key components such as the server, handlers, and routing. It emphasizes the importance of http.ResponseWriter and *http.Request in handling requests and responses, as well as the evolution of HTTP protocols. Additionally, it discusses the Gin framework as an alternative to net/http, addressing its advantages over verbosity and manual processes.

Uploaded by

harshitbatra0602
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

Go Backend

net/http, mux, Gin


Server
● Server
● Port
● Handler
● Router
● JSON response
http
1990s : HTTP/1.1 (by-default, common)

2015 : HTTP/2

2019: HTTP/3
net/http server
net/http
1. Handler
a. [Link]
b. *[Link]
2. Routing: [Link]("/path", handlerFunc)
3. Modern backends don't speak plain text; they speak JSON
4. Mux
a. DefaultServeMux
b. Multiplexer

Key concepts:

● [Link] → response
● *[Link] → request data
● Each request handled in separate goroutine
Two most important objects in Go web development
r *[Link] (The Inbox):

● This is a pointer to a struct holding everything the user sent you.


● The URL they visited, the type of method they used (GET, POST), any query parameters (?name=John),
and the body of their request.

w [Link] (The Outbox):

● This is the interface you use to construct the reply.


● You use it to set status codes (like [Link](404)), set headers, and write the actual data (HTML,
JSON, or Text) back to the user's browser.
● [Link](...): This is the lowest-level, rawest way to send data. It strictly requires a slice of bytes ([]byte).
It's fast but annoying for mixing strings and variables together.
ResponseWriter: Important Interview Pointers
● [Link] implements [Link]
● ResponseWriter = output stream to client
● ResponseWriter is:
○ NOT a buffer you read from
○ NOT a struct you manipulate directly

Note: [Link] is an abstraction over the HTTP response that behaves like an
[Link], so anything that can write bytes can directly send data to the client.
Practice Questions
1. Write a simple net/http server that returns "Hello, Web!" on /.
2. What happens if you don’t call WriteHeader()?
Interview Questions
1. Struct vs Interface
2. Why do we pass ":8080" instead of 8080?
3. How do you send a JSON response in net/http?
4. Does Go support HTTP/2 in net/http?
Gin
Why Gin? Or Pain Points of net/http?

Verbose routing

Manual JSON parsing

No built-in middleware chain

Boilerplate everywhere

You might also like