Creating a Basic Markdown Preview Tool

Powerful Command-Line Applications in Go — by Ricardo Gerardi (30 / 127)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 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:

  1. Read the contents of the input Markdown file.
  2. Use some Go external libraries to parse Markdown and generate a valid HTML block.
  3. Wrap the results with an HTML header and footer.
  4. 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…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.