Golang: HTTP & Networking

Running multiple HTTP servers in Go

In this article, we are going to create some customized HTTP servers and run them at the same time concurrently.

Uday Hiwarale
RunGo
Published in
8 min readFeb 14, 2020

--

(source: pexels.com)

In the Hello World HTTP Server tutorial, we learned about basic APIs to launch an HTTP server in Go. As we’ve learned from this tutorial, once we run http.ListenAndServe() function, our process gets blocked and we can’t run anything below this line.

What if we want to run multiple servers? In this article, we are going to address this question and learn more about server configuration.

If we look at the ListenAndServe function, it has the following syntax.

func ListenAndServe(addr string, handler Handler) error

When we call http.ListenAndServer() with appropriate arguments, it starts an HTTP server and blocks the current goroutine. If we run this function inside the main function, it will block the main goroutine (started by the main function).

💡 If you haven’t read my articles about concurrency & channels, you can follow this link for lesson on goroutines and this link for channels and WaitGroup.

Once you call ListenAndServe function, you do not have another chance to spawn a…

--

--