模板中的 Golang ttf 字体

我试图让 TTF 字体在 golang 模板中工作,但它不会呈现字体。它显示为常规的 Times New Roman。我可以使用标准字体系列字体(ex verdana 或 'helvetica')更改字体,但我无法导入 TTF。

关于 TTF 字体,我似乎只能找到用于向图像添加文本的库,但我想更改网络字体。我怎样才能做到这一点?

项目结构是

  • /html_templates/portal.html

  • /html_teplates/Comfortaa-Regular.ttf

  • 主程序

这是相关的 golang 代码:


偶然的你
浏览 163回答 1
1回答

繁华开满天机

import (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "text/template")type Portal struct{&nbsp; &nbsp; Title string}func main(){&nbsp; &nbsp; //Create MUX Routing handlers&nbsp; &nbsp; http.HandleFunc("/", portal)&nbsp; &nbsp; //Start WebServer&nbsp; &nbsp; if err := http.ListenAndServe(":1234", nil); err != nil{ panic(err) }}func portal(w http.ResponseWriter, r *http.Request){&nbsp; &nbsp; //Create template&nbsp; &nbsp; tmpl, _ := template.ParseFiles("./html_templates/portal.html")&nbsp; &nbsp; //Populate struct&nbsp; &nbsp; portal := Portal{&nbsp; &nbsp; &nbsp; &nbsp; Title: "title",&nbsp; &nbsp; }&nbsp; &nbsp; //Execute template with struct data&nbsp; &nbsp; tmpl.Execute(w, portal)}以及相关的 HTML:<html><head>&nbsp; &nbsp; <title>{{ .Title }}</title>&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <style>&nbsp; &nbsp; &nbsp; &nbsp; @font-face {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-family: 'comfortaaRegular';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; src: url('Comfortaa-Regular.ttf');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; src: local('comfortaaRegular'),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;local('Comfortaa-Regular.ttf'),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url('Comfortaa-Regular.ttf') format('truetype'),&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; body{ font-family: 'comfortaaRegular' }&nbsp; &nbsp; </style></head><body>&nbsp; &nbsp; <p>test/p></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go