Lecture 02 Write Basic Go Web Server
Lecture 02 Write Basic Go Web Server
Server
Lab learning outcomes
After completing this lab you should be able to
● Write basic Go web server program and explain the parts involved such
as http and template packages; HandleFunc, multiplexer,
ListenAndServe, FileServer
Restart the server and refresh your web browser and notice what happen and
explain the reason why
Integrating Bootstrap with Go
Go to the following url and download the Bootstrap library shown under the
Compiled CSS and JS files section
https://getbootstrap.com/docs/4.3/getting-started/download/
In your web root directory (where the server.go file is stored) create a
directory called assets
https://code.jquery.com/jquery-3.3.1.slim.min.js
Save the following CSS code in a file called starter-template.css file and
put it inside the css directory
Integrating Bootstrap with Go
Now your directory structure should look like the
one shown on the right side
At the top inside the head section the Bootstrap and custom .css files are
included
At the bottom before the closing </body> tag Bootstrap and custom .js files
are included
The page was adopted from the Bootstrap example pages (Starter Template)
found in the following URL
https://getbootstrap.com/docs/4.3/examples/
You can find the full code of the customized page in the following URL
https://github.com/betsegawlemma/web-prog-lab-03/blob/
master/index.html
Integrating Bootstrap with Go
Using Go Templates
Use template package in order to serve HTML files
Using Go Templates
If you run your code now, you should see a page similar to the one shown
below
Note that this page is not styled even though the css files were linked in the
index.html page
Serving static files using FileServer
Note the added lines on line 15 and 16
Serving static files using FileServer
If you refresh now you should see the following properly styled page