- [05m] 🏆 Objectives
- [60m] 💻 Work on SSG MVP
- [30m] ✓ Review
- [15m] 🌴 BREAK
- [30m] 📚 Overview: Files & Directories
- [30m] 💻 Work on SSG v1.1
- 📚 Resources & Credits
TODO
Use this time to make progress on your MVP.
Ask class to close laptops. Demonstrate breaking down requirements into function stubs.
Call on individuals to live code their solutions to the first three requirements. Invite the class to analyze each solution and determine why it works.
The ioutil
package we introduced last class period has even more to offer!
It provides a function named ReadDir
, an approachable way to list all the files in the directory. Check out the method signature below:
func ReadDir(dirname string) ([]os.FileInfo, error)
ReadDir
reads the directory named by dirname
, and returns a list of directory entries sorted by filename.
In your $GOROOT
, create a directory named traversing
, with a file inside named main.go
.
Paste the code below, then build and run it. Examine the output and syntax.
package main
import (
"fmt"
"io/ioutil"
"log"
)
func main() {
directory := "."
files, err := ioutil.ReadDir(directory)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
fmt.Println(file.Name())
}
}
To safely ignore returned values from a function, use the _
in place of any variable name.
You might have noticed that the method signature for ReadDir
returns a slice of FileInfo
structs.
Change the value of the directory
variable, and set it to point to your home directory. For example, /Users/YOUR_MAC_USERNAME
.
Click the link in the last sentence, then modify the example code to print out the name of the path and a label indicating if the path is a directory or not (HINT: use IsDir()
).
- ⭐️ IMPORTANT: Finish the requirements for the MVP before beginning v1.1.
- Begin working on the requirements for SSG v1.1. Be sure to copy them into your project's README to keep track of your progress!
- List the files in a folder with Go: How to get a list of files inside a folder on the filesystem using Go.