Working with Markdown in Go (Golang): an overview of popular libraries
Go (Golang) is a modern compiled language often chosen for creating high-performance web applications, CLI utilities, and tools for generating static sites. One typical task is processing Markdown markup. In this article, we will look at three key libraries: blackfriday, goldmark and chroma, and also show how to combine them to create a full-featured Markdown to HTML converter with syntax highlighting.
1. Why process Markdown in Go?
Markdown is a lightweight markup language widely used in documentation, blogs, README files, and content management systems. In the Go ecosystem, there are several mature libraries that allow you to:
- Convert Markdown to HTML for web publishing.
- Add syntax highlighting for code blocks.
- Create custom extensions (e.g., support for LaTeX, diagrams, or custom blocks).
- Integrate with static site generators (Hugo, custom solutions).
2. The blackfriday library (v2)
Blackfriday is one of the oldest and most popular libraries for Go. It is known for its high speed and behavior close to the CommonMark standard. Installation:
go get github.com/russross/blackfriday/v2A simple usage example:
package main
import ( "fmt" "github.com/russross/blackfriday/v2")
func main() { input := []byte("## Hello, Markdown!
This is **bold text** and `code`.") output := blackfriday.Run(input) fmt.Println(string(output))}The result is HTML code with an h2 heading, a paragraph, and strong/code tags. Blackfriday supports extensions: tables, strikethrough, task lists, and autolinks. To activate them, use:
html := blackfriday.Run(input, blackfriday.WithNoExtensions(), blackfriday.WithExtensions(blackfriday.CommonExtensions),)3. The goldmark library (recommended)
Goldmark is a modern Markdown parser written in pure Go. It fully complies with the CommonMark 0.30 specification and provides a flexible API for extensions. Installation:
go get github.com/yuin/goldmarkBasic example:
package main
import ( "bytes" "fmt" "github.com/yuin/goldmark")
func main() { source := []byte("# Heading
Regular text with **bold** and *italic*.") var buf bytes.Buffer if err := goldmark.Convert(source, &buf); err != nil { panic(err) } fmt.Println(buf.String())}Goldmark is easily extensible. Built-in extensions include tables, task lists, footnotes, autolinks, and code highlighting via chroma. Example with the table extension:
import ( "github.com/yuin/goldmark" "github.com/yuin/goldmark/extension")
markdown := goldmark.New( goldmark.WithExtensions(extension.Table),)4. Code highlighting with chroma
Chroma is a syntax highlighting library for Go that supports over 190 programming languages and many themes. Installation:
go get github.com/alecthomas/chroma/v2Integration with goldmark looks like this:
package main
import ( "bytes" "fmt" "github.com/alecthomas/chroma/v2/quick" "github.com/yuin/goldmark" highlighting "github.com/yuin/goldmark-highlighting/v2")
func main() { source := []byte("```gopackage main
import \\"fmt\\"
func main() { fmt.Println(\\"Hello, world!\\")}```") markdown := goldmark.New( goldmark.WithExtensions( highlighting.Highlighting, Also in library
Caching in Node.js: Redis and Alternatives for Accelerating Applications
PHP Task Scheduler: cron-expression Library for Flexible Scheduling
gRPC for Kotlin: Libraries, Server, and Client — Code Examples
OAuth client for C# (.NET): IdentityModel library and OAuth 2.0 integration