I'm trying to get a TTF font to work in a golang template, but it wont render the font. It shows up as regular Times New Roman. I can change fonts using the standard font-family fonts (ex verdana or 'helvetica'), but I cant import a TTF.
All I can seem to find about TTF fonts is libraries to add text to images, but I want to change web fonts. How can I achieve this?
Structure of project is
- /html_templates/portal.html
- /html_teplates/Comfortaa-Regular.ttf
- main.go
Here is the relevant golang code:
import (
"fmt"
"net/http"
"text/template"
)
type Portal struct{
Title string
}
func main(){
//Create MUX Routing handlers
http.HandleFunc("/", portal)
//Start WebServer
if err := http.ListenAndServe(":1234", nil); err != nil{ panic(err) }
}
func portal(w http.ResponseWriter, r *http.Request){
//Create template
tmpl, _ := template.ParseFiles("./html_templates/portal.html")
//Populate struct
portal := Portal{
Title: "title",
}
//Execute template with struct data
tmpl.Execute(w, portal)
}
And the relevant HTML:
<html>
<head>
<title>{{ .Title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
@font-face {
font-family: 'comfortaaRegular';
src: url('Comfortaa-Regular.ttf');
src: local('comfortaaRegular'),
local('Comfortaa-Regular.ttf'),
url('Comfortaa-Regular.ttf') format('truetype'),
}
body{ font-family: 'comfortaaRegular' }
</style>
</head>
<body>
<p>test/p>
</body>
</html>