Posted on

Go fonts for the web

If you like Go fonts and want to have them on your site backed by the go web server, now it's easier than ever.

Go fonts are not for everyone: some love them, some don't. Still, I find these fonts to be readable. They have a specific character and a liberal license. If you want to use them in your code or get regular TTF files for local usage, you can find all that under the golang.org/x/image repository.

What's lacking there, however, is an easy way to integrate those fonts with the web. Package artyom.dev/gofontweb addresses such a need. You can install it as:

go get artyom.dev/gofontweb@latest

In your Go-based web server code, expose the embedded filesystem as a dedicated handler:

mux := http.NewServeMux()

// http.Handler serving font files and CSS declaring their font families
fontsHandler := http.FileServer(http.FS(gofontweb.FS()))
// make it available under some prefix
mux.Handle("/fonts/", http.StripPrefix("/fonts/", fontsHandler))
// set up other routes
mux.Handle("/", ...)

Then in HTML code of your pages load “go.css” stylesheet to declare Go font families:

<link rel=stylesheet href="/fonts/go.css">

That stylesheet declares the following font families:

That's it! If you'd rather have the woff2 files, you can find them in the repository.