- [05m] 🏆 Objectives
- [10m] 💻 Warm Up: Explore SSGs
- [10m] 📖 Overview: Static Sites & Generators
- [10m] 📖 Starting a New Project
- [05m] 📖 Building, Running, & Installing
- [10m] 📖 CLIs & Standard Input/Output (I/O)
- [15m] 🌴 BREAK
- [10m] 📖 Reading & Writing to the Filesystem
- [10m] 📖 Working with Templates
- [65m] 💻 Activity: Complete MVP
- [15m] 📖 Project Checkin
- 📚 Resources & Credits
- Define what a Static Site Generator is, and what it's best-fit use cases are.
- Demonstrate setting up a Golang project in an IDE.
- Explore packages in the standard library that read and write to the command line, enable file access, and custom templating.
- Apply the syntax rules from the tutorial in a real world project.
First, write down as many differences you can think of between static sites and dynamic sites.
Then, use staticgen.com to research the following questions. If you have extra time, discuss your answers with your peers.
- What problem(s) can a static site generator solve?
- As a developer, what could you use a static site generator for?
Four key differences from dynamic sites:
- No server-side language
- No database
- Composed of only HTML, CSS, and JavaScript
- Site files are delivered to the end user exactly as they are on the server
- Intermediary between static and dynamic sites
- Run a command to turn text files into HTML pages
- Commit the generated, static HTML pages to GitHub
- Serve the HTML from GitHub Pages
- Blogs
- Portfolios
- Marketing Websites
- Course Websites (like this one!)
- Research Journals
- Daily Development Logs
- Speed
- Security
- Content can be version controlled
- Don't have to deal with a server
- Can handle lots of sudden internet traffic
- No real-time content
- No user input
- No administrative user interface
David Walsh blogs more about the specifics in An Introduction to Static Site Generators. Be sure to read over this before you start your project!
Follow the Getting Started section of the project README
.
Execute the following in command in your project's root directory:
To build your program, run:
go build
To run the latest build:
./makesite
To install it for use system wide, run:
go install
fmt.Print()
fmt.Println()
fmt.Sprintf()
flag
package offers a standard library for parsing command line input.- Defined using
flag.String()
,Bool()
,Int()
, etc. - Syntax:
-example
,-example=text
,-example text
. - Both
-
and--
may be used for flags.
examplePtr := flag.String("example", "defaultValue", " Help text.")
examplePtr
will reference the value for either -example
or --example
.
The initial value at *examplePtr
is defaultValue
.
Calling flag.Parse()
will parse the command line input and write the value following -example
or --example
to *examplePtr
.
package main
import (
"fmt"
"io/ioutil"
)
func main() {
fileContents, err := ioutil.ReadFile("first-post.txt")
if err != nil {
// A common use of `panic` is to abort if a function returns an error
// value that we don’t know how to (or want to) handle. This example
// panics if we get an unexpected error when creating a new file.
panic(err)
}
fmt.Print(string(fileContents))
}
package main
import (
"fmt"
"io/ioutil"
)
func main() {
bytesToWrite := []byte("hello\ngo\n")
err := ioutil.WriteFile("new-file.txt", bytesToWrite, 0644)
if err != nil {
panic(err)
}
}
type entry struct {
Name string
Done bool
}
type ToDo struct {
User string
List []entry
}
<!DOCTYPE html>
<html>
<head>
<title>Go To-Do list</title>
</head>
<body>
<p>
To-Do list for user: {{ .User }}
</p>
<table>
<tr>
<td>Task</td>
<td>Done</td>
</tr>
{{ with .List }}
{{ range . }}
<tr>
<td>{{ .Name }}</td>
<td>{{ if .Done }}Yes{{ else }}No{{ end }}</td>
</tr>
{{ end }}
{{ end }}
</table>
</body>
</html>
package main
import (
"html/template"
"os"
)
type entry struct {
Name string
Done bool
}
type ToDo struct {
User string
List []entry
}
func main() {
t := template.Must(template.New("template.tmpl").ParseFiles("new.html"))
err = t.Execute(os.Stdout, todos)
if err != nil {
panic(err)
}
}
Complete the MVP as described in the requirements in the order they appear. If you finish early, move on to the stretch challenges.
Return 15 minutes before the end of class for your project status update.
Students will check in with the instructor on the status of their MVP.
This is a great time to ask questions or get unblocked so you can make progress before our next class!
- Static Site Generators: Modern Tools for Static Website Development - A free eBook all about modern-day static website development, and the backend tools required.
- David Walsh: An Introduction to Static Site Generators - A detailed look at the features, advantages, and disadvantages of static site generators.
- Go By Example: Reading Files
- Go By Example: Writing Files
- Go By Example: Panic
- GopherAcademy: Using Go Templates
- rapid7.com: Building a Simple CLI Tool with Golang