Skip to Content

Project 2: CRUD API

CRUD for struct data and using MUX


For my second day of the project challenge, I continued to explore the capabilities of the Go programming language. While I had some experience with Go from the previous day's project, I wanted to take my knowledge to the next level by delving deeper into its features and functionalities.

This project will cover on how to use the five most common HTTP Method which is GET, POST, PUT, PATCH, and DELETE to alter data in the server. But I am not using any database yet since I think it will take its own project, instead I use some kind of struct and series which  I will talk about later. This project will also trying to use a router framework like gorilla mux in here

From what I read, the net/http package is already enough at providing everything you need to get your own REST APIs up and running. However, when dealing with attractive URLs that utilize variables, a custom multiplexer is required, so in gorilla mux, this functionality comes built-in at no additional work.

The source code will be on my github.

Here is some of the think that I learned when creating my project

Importing external pakcage

So just like NodeJS with NPM and Python with pip, Golang can also download an external package through command prompt using go get {url-package}. For example below is how I download a package for gorilla mux:

go get "github.com/gorilla/mux"

After that, the package can be imported just like the other package but write the full name with the url ("github.com/gorilla/mux").

I also notice a change in go.mod file and a new file will be created named go.sum. According to Golang official page,  The go.mod file defines the module’s module path, which is also the import path used for the root directory, and its dependency requirements, which are the other modules needed for a successful build. While the go.sum file will contains cryptographic hashes of the module’s direct and indirect dependencies. When the go command downloads a module .mod or .zip file into the module cache, it computes a hash and checks that the hash matches the corresponding hash in the main module’s go.sum file.

So by that definition, I think it make sense when we download an external package, both file will be updated, and I like to think about it just like package.json and package-lock.json in NodeJS.

Below is the go.mod and go.sum file after downloading the mux package:




Routing with mux

As I mention earlier, with framework like mux, we can assign a route with a HTTP method simply. below is the code for each of the five common HTTP method.

as you can see, a new variable is declared named "router" that will contain the mux router object, and we can assign route with "HandleFunc" and "Method" for the HTTP method. On the last line, we will assign that "router" variable as the listener on the server using http.ListenAndServe.

Declaring Struct and Series

This is actually just a basic syntax, but I just want to write it here since for me its actually a unique way (compared to python atleast) to declare a series and struct. Series is a data structure of an array of element and struct is a type created by the user to hold various fields in one single field.

Below is the example code on how to declare struct and series.

So basically, there is a struct called "User" and it will contain three string field which is Fullname, UserName, and Email. The last line is declaring a series variable named "users" that will contaion User struct data type. To create a new User data and add it to the users list, we can use the code like below.

user := User{FullNme: "Sean Joe", UserName: "SJ", Email: "Sean@mail"}
users = append(users, user)

Conclusion

​In conclusion, my project challenge has been an exciting and fruitful experience. It has allowed me to delve deeper into Golang, and develop my skills as a programmer. In just a short amount of time, I was able to build a web application with dynamic routing and URL parameters, demonstrating the power and flexibility of these technologies. 

There is actually a lot of thing that I learned outside what I wrote on this post like how to get the parameter on URL, using content-type, encode and decode a json data, and many more. But I am afraid that I cant write it all to this blog, because I am trying to focus on spending my time learning first and it is a lot of work.

I am excited to see what else I can achieve with Go, and look forward to tackling new challenges as I continue to learn and grow. 

Stay tuned for the next blog post, where I'll be sharing my progress on day three of this project challenge.

Project 1: Getting Started
Simple Web Server with Golang