If you have been reading our blogs, then you are already aware how to create a RESTful API using net/http package, gorilla mux library and gin framework. And you have also seen how Golang for app development is rapidly becoming a trend. In this tutorial, let us get one step further and create a simple todo app. That’s right! An app that will help you schedule your daily tasks and once it has been accomplished, you can tick them off.
So, let us get started.
We are basically going to create a todo list app. We will further take it up a notch by incorporating static assets like CSS and JavaScript and templates. And in order to write the code down, you need to have a text editor or an IDE. In this case, we will be using the VS Code.
cd go-workspace
mkdir todo
cd todo
code .
package main
import (
"html/template"
"log"
"net/http"
)
var tmpl *template.Template
type List struct {
Object string
Finish bool
}
type PageInfo struct {
Title string
Todos []List
}
var tmpl *template.Template
. We are calling this tmpl, and we are making a pointer and a template object. Be careful to import the html/template package for this.func list(w http.ResponseWriter, r *http.Request) {
data := PageInfo {
Title: "ToDo List",
Todos: []List{
{Object: "Write scripts", Finish: true},
{Object: "Shoot video", Finish: false},
{Object: "Edit the video", Finish: false},
},
}
tmpl.Execute(w, data)
}
w http.ResponseWriter, r *http.Request
.We can see that we are using a response writer and pointer to the request object. The ResponseWriter interface plays a pivotal role in construction of an HTTP response. The Request method mentions the HTTP methods, i.e, GET, POST, etc.Todos: []List{
{Object: "Write scripts", Finish: true},
{Object: "Shoot video", Finish: false},
{Object: "Edit the video", Finish: false},
},
func main() {
mux := http.NewServeMux()
tmpl = template.Must(template.ParseFiles("templates/mark.gohtml"))
fs := http.FileServer(http.Dir("./static"))
mux.Handle("/static/", http.StripPrefix("/static/", fs))
mux.HandleFunc("/list", list)
log.Fatal(http.ListenAndServe(":8080", mux))
}
mux := http.NewServeMux.
The NewServeMux is responsible for allocating and returning a brand new ServeMux. The ServeMux is an HTTP request multiplexer. It invokes the handler after checking each incoming request's URL against a register of patterns.mux.HandleFunc("/list", list)
tmpl = template.Must(template.ParseFiles(“templates/mark.gohtml”))
, we are instantiating tmpl. The template.Must is crucial to wrap around the templates. If it is not there, then the program will crash as it didn’t get the required templates. Next, we wrote template.ParseFiles which represents the string of templates.log.Fatal(http.ListenAndServe(":8080", mux))
. We are binding the server to 8080.fs := http.FileServer(http.Dir("./static"))
mux.Handle("/static/", http.StripPrefix("/static/", fs))
With this, the main function comes to an end.
Meanwhile, if you are stuck with a project, then you should not hesitate in hiring experts who will choose Golang for app development. They will guide you how to write the code precisely and efficiently.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/static/css/format.css">
<title>TODO</title>
</head>
<body>
<h1>{{ .Title }}</h1>
<ul>
{{ range .Todos }}
{{ if .Finish }}
<li class="finish">{{.Object}}</li>
{{ else }}
<li>{{.Object}}</li>
{{ end }}
{{ end }}
</ul>
<script src="/static/js/script.js" type="text/javascript"></script>
</body>
</html>
Next, we will work on the static folder (which is present inside the directory ‘todo’), which consists of css and js folders. The css folder in turn consists of format.css file and the js folder consists of the script.js file.
We type the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
body {
background-color: orange;
color: steelblue;
width: 50%;
margin: 0 auto;
}
ul li {
cursor: pointer;
}
.finish {
text-decoration: line-through;
}
The code that is written in this section is simple enough to understand. We have just typed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let objects = document.getElementsByTagName("li")
for (let i=0; i<objects.length; i++) {
objects[i].addEventListener("click", () => {
objects[i].classList.toggle("finish")
})
}
Finally, we will run the whole code. For this, we go to our Terminal and type go run main.go. Then as we open a browser and type localhost:8080/list, we get to see the following snapshot.
This means that the program is successful and we have gotten our desired output. In this tutorial, not only did you learn to code in Golang, but also how to code in css and JavaScript. In a way, you learned about frontend and backend development. How cool is that?
In order to get access to the whole code, you should visit: https://github.com/GolangCompany/todo-app.