Server-Side Scripting/Routes and Templates/Go
app.go
edit// Demonstrates a complete server-side website using:
// * static HTML and CSS
// * a template
// * a code module
//
// NOTE: Static pages (html, css, images, etc.) are placed in
// a folder named "static". Template pages are placed in a folder
// named "templates". Code modules are placed in a folder named
// "routes".
//
// Folder structure:
// app.go
// go.mod
// routes
// lesson1.go
// lesson2.go
// static
// index.html
// hello.html
// styles.css
// templates
// lesson2.html
//
// References:
// https://gowebexamples.com/hello-world/
// https://www.honeybadger.io/blog/go-web-services/
// https://linguinecode.com/post/how-to-import-local-files-packages-in-golang
// https://www.alexedwards.net/blog/serving-static-sites-with-go
package main
import (
"log"
"net/http"
"local/routes"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/lesson1", routes.Lesson1)
mux.HandleFunc("/lesson2", routes.Lesson2)
mux.Handle("/",
http.FileServer(http.Dir("./static")))
log.Fatal(http.ListenAndServe(":8000", mux))
}
go.mod
editmodule local
go 1.17
routes/lesson1.go
edit// Displays "Hello code world!"
//
// References:
// https://repl.it/@mauriycf/A-simple-web-server-in-go#main.go
// https://gowebexamples.com/hello-world/
package routes
import (
"net/http"
"io"
)
func Lesson1(response http.ResponseWriter, request *http.Request) {
io.WriteString(response, "Hello code world!")
}
routes/lesson2.go
edit// Displays a rendered template.
//
// References:
// https://blog.gopheracademy.com/advent-2017/using-go-templates/
// https://gowebexamples.com/templates/
package routes
import (
"net/http"
"html/template"
"path/filepath"
)
func Lesson2(response http.ResponseWriter, request *http.Request) {
type Data struct {
Greeting string
Name string
}
data := Data{"Hello", "world"}
path := filepath.Join("templates", "lesson2.html")
parsed, _ := template.ParseFiles(path)
parsed.Execute(response, data)
}
static/hello.html
edit<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<p>Hello static world!</p>
</body>
</html>
static/index.html
edit<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Routes and Templates</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello World</h1>
<ul>
<li><a href="hello.html">Static</a></li>
<li><a href="lesson1">Code</a></li>
<li><a href="lesson2">Template</a></li>
</ul>
</body>
</html>
static/styles.css
editbody {
color: blue;
font-family: Arial, Helvetica, sans-serif;
font-size: larger;
}
templates/lesson2.html
edit<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Lesson 2</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<p>{{.Greeting}} template {{.Name}}!</p>
</body>
</html>
Try It
editCopy and paste the code above into the following free online development environment or use your own Go compiler / interpreter / IDE.