Hello! ๐
In this tutorial I will show you how to build a CRUD app using the Go programming language. By the end, you'll have a fully functioning CRUD (Create, Read, Update, Delete) app running locally. Let's get started.
Create a new directory for the project and navigate into it:
mkdir crud-app && cd crud-app
Next we will initialize the go module and install the dependencies:
go mod init go-crud-app
go get -u github.com/gorilla/mux
Now that the project and required packages are installed we can start working on the application. ๐ธ
Create a new file called "main.go" and import the following:
package main
import (
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
Next we define our global variables like so:
var memos []Memo
var idCounter int
Now we need to create a struct to define a memo:
type Memo struct {
ID int `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
}
Next we will create handlers for each of the CRUD operations, the first one will handle create a new memo:
func createMemo(w http.ResponseWriter, r *http.Request) {
var memo Memo
json.NewDecoder(r.Body).Decode(&memo)
idCounter++
memo.ID = idCounter
memos = append(memos, memo)
json.NewEncoder(w).Encode(memo)
}
The next function will handle getting all the memos:
func getMemos(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(memos)
}
Below is the handler to handle getting a single memo:
func getMemo(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
id, _ := strconv.Atoi(params["id"])
for _, memo := range memos {
if memo.ID == id {
json.NewEncoder(w).Encode(memo)
return
}
}
http.Error(w, "Memo not found", http.StatusNotFound)
}
The next function handles updating a memo:
func updateMemo(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
id, _ := strconv.Atoi(params["id"])
for i, memo := range memos {
if memo.ID == id {
json.NewDecoder(r.Body).Decode(&memo)
memo.ID = id
memos[i] = memo
json.NewEncoder(w).Encode(memo)
return
}
}
http.Error(w, "Memo not found", http.StatusNotFound)
}
Lastly we need a function to handle the delete operation:
func deleteMemo(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
id, _ := strconv.Atoi(params["id"])
for i, memo := range memos {
if memo.ID == id {
memos = append(memos[:i], memos[i+1:]...)
json.NewEncoder(w).Encode("The memo was deleted successfully")
return
}
}
http.Error(w, "Memo not found", http.StatusNotFound)
}
Next we need to set up the routes to handle each of the CRUD requests:
func initializeRouter() {
router := mux.NewRouter()
router.HandleFunc("/memos", createMemo).Methods("POST")
router.HandleFunc("/memos", getMemos).Methods("GET")
router.HandleFunc("/memos/{id}", getMemo).Methods("GET")
router.HandleFunc("/memos/{id}", updateMemo).Methods("PUT")
router.HandleFunc("/memos/{id}", deleteMemo).Methods("DELETE")
log.Fatal(http.ListenAndServe(":8000", router))
}
Finally we need to implement the main function, as so:
func main() {
memos = append(memos, Memo{ID: 1, Title: "First memo", Content: "Hello World"})
idCounter = 1
initializeRouter()
}
Done! Now we can move onto testing the application. ๐
First we need to start the server before we can make requests to it, this is done via the following command:
go run main.go
Now we can use the following CURL commands to test each of the endpoints.
Create a memo:
curl -X POST -d '{"title":"New Memo","content":"This is a new memo."}' -H "Content-Type: application/json" http://localhost:8000/memos
Get all memos:
curl http://localhost:8000/memos
Get a memo by it's ID:
curl http://localhost:8000/memos/1
Update a memo:
curl -X PUT -d '{"title":"Updated Memo","content":"This is an updated memo."}' -H "Content-Type: application/json" http://localhost:8000/memos/1
Delete a memo:
curl -X DELETE http://localhost:8000/memos/1
Feel free to change the contents and have a play around with it. ๐
In this tutorial I have shown how to implement a CRUD application using the Go programming language, I'm having a lot of fun learning Go and I hope this tutorial has helped you.
As always you can find the sample code on my Github: https://github.com/ethand91/go-crud
Happy Coding! ๐
Like my work? I post about a variety of topics, if you would like to see more please like and follow me. Also I love coffee.
If you are looking to learn Algorithm Patterns to ace the coding interview I recommend the [following course](https://algolab.so/p/algorithms-and-data-structure-video-course?affcode=1413380_bzrepgch