Implementing a Go-based syntax highlighter for code snippets involves a few steps.
We will use a library called Chroma, which is a general-purpose syntax highlighter in pure Go, supports over 150 languages, and has a variety of styles.
Follow the step-by-step guide below:
Ask your specific question in Mate AI
In Mate you can connect your project, ask questions about your repository, and use AI Agent to solve programming tasks
Step 1: Install and Import Chroma Package
The first step is to install the Chroma Go package. You can use go get
to download the package. Open your terminal and copy-paste the following command:
go get -u github.com/alecthomas/chroma
After successfully downloading the package, you can import it in your Go file as follows:
import (
"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/styles"
)
Step 2: Define The Code To Be Highlighted
// Code to be highlighted
code := `
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
`
Step 3: Get a Lexer
Using Chroma, you start by getting or identifying the lexer that corresponds to the language of the code to be highlighted.
// Identify the lexer
lexer := lexers.Get("go")
// But it's more recommended to use this function as it auto-detects the language
lexer = chroma.Coalesce(lexer)
Step 4: Get a Style and a Formatter
Next, we need to get the styles and formatter that we want to use for the highlighted code. For the purpose of this guide, let's use the "monokai" style and the "html" formatter.
// Use "monokai" style
style := styles.Get("monokai")
if style == nil {
style = styles.Fallback
}
// Create an HTML formatter
formatter := html.New(html.WithClasses())
Step 5: Tokenize and Highlight Code
Finally, we tokenize the code and then use the formatter to convert these tokens into stylized, colored text.
// Tokenize the source code
iterator, _ := lexer.Tokenise(nil, code)
// Generate HTML
err := formatter.Format(w, style, iterator)
if err != nil {
log.Fatal(err)
}
You'll just replace w
with an io.Writer
where the generated HTML will be written. If you want to output to the console, you can use os.Stdout
or if you want to write to a string, you can use a bytes.Buffer
.
Full Example:
package main
import (
"bytes"
"log"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/styles"
)
func main() {
code := `
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}`
// Identify the lexer
lexer := lexers.Get("go")
// Use "monokai" style
style := styles.Get("monokai")
// Create an HTML formatter
formatter := html.New(html.WithClasses())
// Tokenize the source code
iterator, _ := lexer.Tokenise(nil, code)
// Generate HTML
var b bytes.Buffer
err := formatter.Format(&b, style, iterator)
if err != nil {
log.Fatal(err)
}
// Output the generated HTML
log.Println(b.String())
}
With this full example, you can compile and run this Go program, and it will output the HTML of your syntax-highlighted Go code. You may modify this to output or write the HTML to a file or serve it on a web page.
That's all! You have successfully implemented a Go-based syntax highlighter for code snippets.
AI agent for developers
Boost your productivity with Mate:
easily connect your project, generate code, and debug smarter - all powered by AI.
Do you want to solve problems like this faster? Download now for free.