Serving static files
In the previous chapters, you have seen how to render API responses in JSON and XML formats. In this chapter, you will learn how you can render different types of responses. We will start by serving a basic index.html
file and then move on to serving static files, such as JavaScript, CSS files, and images from a filesystem, and eventually render HTML templates.
To start serving static files, follow these steps:
- Create a new project folder and open it with the VSCode IDE. Then, create an
index.html
file to display aHello world
message with an<h2>
tag, as follows:<html> <head> <title>Recipes Platform</title> </head> <body> <h2>Hello world</h2> </body> </html>
- Next, with the
go get
command, installgithub.com/gin-gonic/gin
, write amain.go
file, and define a router with thegin.Default()
method. Then, define a route for theindex
page and register...