Creating GOLang Project, Server and URL paths

Ruminder Singh
Geek Culture
Published in
3 min readJun 30, 2021

--

In recent years, GoLang has become popular backend language for web development. In this article I will tell you

  1. How to create GoLang project.
  2. Creating server, routes and pages using net/http module.
  3. Separating the code into api files for large number of paths.

For practise you can find the sample code on this link.

Creating the Project

Someone can find creating project, importing the files as complicated, but once you know it is very easy. Install the go on your laptop either using brew install go or from the Golang website.

Create folder for your project.

mkdir golang-http
cd golang-http

Go.mod file

Create Go.mod file. GoLang uses go.mod file to track all the dependencies, libraries you are going to use in your application similar to package.json. While creating we pass path to it which will be your module or application path, so that if you want other developers to use their module, they can use them. Most of the time we use github.com as our code will be on it.

go mod init github.com/username/golang-http

Main.go

Now create the main.go file which will contain the main function, our starting point of the application. The first line of any file should be package packageName.

Creating Routes

To create API routes, we will use net/http package. You can also use mux or other libraries which internally implement interfaces of the net/http.
To handle the request, you need to create a handler and then the path or route for which that handler will run. Think of handler as controller which will run for particular path.

func welcome(writer http.ResponseWriter, request *http.Request){
io.WriteString(writer, "<h1>Welcome to first goLang
application\n</h1>")
}

This method will return The above method will write the output as response of the request. You can use other library like fmt for output.

func hello(writer http.ResponseWriter, request *http.Request) {
fmt.Fprintf(writer, "Hello")
fmt.Fprintf(writer, "<h1>First GoLang Application</h1>")
}

If you want to use html tags, make sure they are added first, else the output will be normal text.

Server Creation and adding handler to routes

In main function we will write all the request handlers and server initialization as we want our server up on running main function.

func main() {
http.HandleFunc("/", welcome)
http.HandleFunc("/hello", hello)
log.Fatal(http.ListenAndServe(":3000", nil))
}

ListenAndServer method of http will start server at given port and handler which is kept nil for starting. We have passed it to the log.Fatal incase error occurs, it will print the logs. Here for address “/” we will be calling welcome method. http. HandleFunc tell all the requests to “/hello” will be handled by hello method.

API Layer creation

With time, the size of application will increase and so does the routes. Adding all the route handlers into main file will make it messy and difficult to debug. We will separate the handlers based on the routes and add all the routes with same base route into single file. Create folder handlers and create files based on routes i.e apiHandler.go. Add all the methods with base url “/api” to it. To use the functions outside of the package (package is folder ), method names must start with capital letter.

package handlers
import (
"fmt"
"io"
"net/http"
)
func WelcomeAPI(writer http.ResponseWriter, request *http.Request) {
io.WriteString(writer, "<h1>Welcome to first goLang
application\n</h1>")
}
func HelloAPI(writer http.ResponseWriter, request *http.Request) {
fmt.Fprintf(writer, "Hello")
fmt.Fprintf(writer, "<h1>First GoLang Application</h1>")
}

To import the files we will use the path which we used while creating the go.mod file.

package handlers
import (
"fmt"
"github.com/username/golang-http/handlers"
)

And use http.HandleFunc(“/api/”, handlers.WelcomeAPI) to use it for API function.

I hope you get an idea on how to start working on project with GoLang. For any questions drop me a comment. Best of Luck with GoLang.

--

--