Build Basic HTTP Server in Go

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This challenge aims to test your ability to write a basic HTTP server using the net/http package in Golang.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/NetworkingGroup(["`Networking`"]) go/NetworkingGroup -.-> go/http_server("`HTTP Server`") subgraph Lab Skills go/http_server -.-> lab-15402{{"`Build Basic HTTP Server in Go`"}} end

HTTP Server

You are required to write a simple HTTP server that can handle two routes: /hello and /headers. The /hello route should return a simple "hello" response, while the /headers route should return all the HTTP request headers.

Requirements

  • The server should use the net/http package.
  • The /hello route should return a "hello" response.
  • The /headers route should return all the HTTP request headers.
  • The server should listen on port 8090.

Example

## Run the server in the background.
$ go run http-servers.go &

## Access the `/hello` route.
$ curl localhost:8090/hello
hello

Summary

In this challenge, you were required to write a simple HTTP server that can handle two routes: /hello and /headers. You learned how to use the net/http package to write handlers for each route and register them on the server. Finally, you learned how to start the server and listen for incoming requests.

Other Go Tutorials you may like