This tutorial would show you how to create a basic http server in Go lang.
For this we’ll need Go 1.22 as it offers built-in enhanced routing that would save us time in thinking what framework should we use.
Folder Structure
go.mod
go.work
main.go
handlers/
home.go
articles.go
Initilizing Project
Create project.
Initialize project.
Creating the router
NewServeMux would return a ServeMux that would serve us our router for the application.
Let’s create barebone http server first to try it out.
Method Specific Route
To filter specific request method to a route, we could use a prefix to identify the target request method.
One of the biggest game changer introduced in Go 1.22 is the ability to read url paths.
Register the path:
What do we have now?
Testing the new route
Organizing our project
To better organize our code, we’ll create a new module.
Then move our route to each separate module inside handlers
Update our main.go to read the handlers submodule.
Conclusion
Here we have learned about how to create a basic HTTP server using Go and how to utilize.
Hoping that this tutorial helped kickstart your dream project in Go.
To see the full project in this tutorial click here.