Creating a Basic Markdown Preview Tool
Powerful Command-Line Applications in Go — by Ricardo Gerardi (30 / 127)
👈 Chapter 3 Working with Files in Go | TOC | Writing Tests for the Markdo wn Preview Tool 👉
Let’s implement the initial version of the Markdown Preview tool. We’ll call this tool mdp (for MarkDown Preview) and accept the file name of the Markdown file to be previewed as its argument. This tool will perform four main steps:
- Read the contents of the input Markdown file.
- Use some Go external libraries to parse Markdown and generate a valid HTML block.
- Wrap the results with an HTML header and footer.
- Save the buffer to an HTML file that you can view in a browser.
When writing Go programs, you can organize your code in many ways. For this tool, you’ll keep all the code in a single package main since this functionality won’t be used outside the CLI implementation. In Chapter 4, Navigating the File System, you’ll see how to organize your code as a single package with many files. For now, you’ll keep the code in main.go.
You already know that to create executable files in Go, the program has to start from the main function in the main package. But having all the code inside the main function is inconvenient and makes it harder to automate testing. To address this issue, a common pattern is to break the main function into smaller focused functions that…