Css 模板中的Golang ttf字体
我试图让TTF字体在golang模板中工作,但它不会呈现字体。它以《新罗马时报》的形式出现。我可以使用标准字体系列字体(例如verdana或“helvetica”)更改字体,但无法导入TTF 我所能找到的关于TTF字体的所有信息都是用于向图像添加文本的库,但我想更改web字体。我怎样才能做到这一点 项目结构如下:Css 模板中的Golang ttf字体,css,templates,go,truetype,Css,Templates,Go,Truetype,我试图让TTF字体在golang模板中工作,但它不会呈现字体。它以《新罗马时报》的形式出现。我可以使用标准字体系列字体(例如verdana或“helvetica”)更改字体,但无法导入TTF 我所能找到的关于TTF字体的所有信息都是用于向图像添加文本的库,但我想更改web字体。我怎样才能做到这一点 项目结构如下: /html\u模板/portal.html /html_teplates/Comfortaa-Regular.ttf 梅因,加油 以下是相关的golang代码: import (
- /html\u模板/portal.html
- /html_teplates/Comfortaa-Regular.ttf
- 梅因,加油
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)
}
以及相关的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>
{{.Title}
@字体{
字体系列:“舒适性规则”;
src:url('Comfortaa-Regular.ttf');
src:local('comfortaaRegular'),
本地('AA-Regular.ttf'),
url('Comfortaa-Regular.ttf')格式('truetype'),
}
正文{font系列:'ComfortaeARegular'}
测试/p>
您需要处理静态文件,将其添加到main func,并在模板中将url设置为/static/courtaa Regular.ttf
//Create MUX Routing for static
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
下面是完整的工作代码
package main
import (
"net/http"
"text/template"
)
type Portal struct{
Title string
}
func main(){
//Create MUX Routing handlers
http.HandleFunc("/", portal)
//Create MUX Routing for static
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
//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)
}
和模板
<head>
<title>{{ .Title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
@font-face {
font-family: 'comfortaaRegular';
src: url('/static/Comfortaa-Regular.ttf');
src: local('comfortaaRegular'),
local('Comfortaa-Regular.ttf'),
url('/static/Comfortaa-Regular.ttf') format('truetype'),
}
body{ font-family: 'comfortaaRegular' }
</style>
</head>
<body>
<p>test</p>
</body>
</html>
{{.Title}
@字体{
字体系列:“舒适性规则”;
src:url('/static/Comfortaa-Regular.ttf');
src:local('comfortaaRegular'),
本地('AA-Regular.ttf'),
url('/static/Comfortaa Regular.ttf')格式('truetype'),
}
正文{font系列:'ComfortaeARegular'}
试验
您是否尝试过谷歌提供的示例?包括对https://fonts.googleapis.com/css?family=[family name]
这是您缺少的,更简单的font family=[family name]
参考。[链接:我不知道我必须为静态内容创建一个文件服务器!谢谢!