Go Series: Learning How to Build a Web Service Using Go from Scratch Pt. 14 — Routes

Jonathan Chao
2 min readJul 16, 2023

We’ve got our controller. Now it’s just a matter of fact that we need to connect it to some route.

Remember we have a UserController struct and NewUserController method to create this struct.

Since we have defined the routes already as such

func NewUserController(r *gin.Engine, userManager *managers.UserManager) {
userController := UserController {
Manager: userManager
}
userGroup := r.Group("/user") // localhost:8080/user/......
// mapping localhost:8080/user/createorupdate to userController.CreateOrUpdateUser
userGroup.POST("/createorupdate", userController.CreateOrUpdateUser)

We just need to connect this userGroup to the main route and call it a day. After that, say our domain is localhost:8080 , we should be able to make a call to localhost:8080/user/createorupdate .

In the route.go , we define a method. Let’s call it SetupRoutes.

func SetupRoutes(r *gin.Engine) {
controllers.NewUserController(r, UserManager)
}

But then we also need to initialize UserManager

func SetupRoutes(r *gin.Engine) {
UserManager := managers.NewUserManager(userRepo)
controllers.NewUserController(r, UserManager)
}

--

--

Jonathan Chao

I am a software developer who has been in this industry for close to a decade. I share my experience to people who are or want to get into the industry