如何在beego上添加favicon.ico?

How to add favicon.ico on beego?

my routers/default.go,我正在尝试使用原始的Go解决方案,但是失败了,这段代码无法编译。我不知道如何用faviconHandler替换路由器:

func faviconHandler(w http.ResponseWriter, r *http.Request) {                                                                                                                                 
        http.ServeFile(w, r, "static/img/favicon.ico")                                                                                                                                        
}                                                                                                                                                                                             
func init() {                                                                                                                                                                                 
        beego.Router("/", &controllers.MainController{})                                                                                                                                      
        beego.Router("/favicon.ico", faviconHandler)  // this doesn't work                 
}

我发现至少有一种方法:嵌入到 index.tpl。

bee new newapp
cd $GOPATH/src/newapp
bee run  # you will see the favicon of a bee.

将$GOPATH/src/view/index.tpl中的内嵌文字修改成你的,这里是linux脚本

cd views
# assume you have put the favicon.ico in this directory
base64 -w0 favicon.ico > favicon.b64
cp index.tpl index.tpl.old
sed 's/base64,.*"/base64,\n"/' index.tpl.old | sed '7r favicon.b64' > index.tpl
# rm favicon.ico favicon.b64  index.tpl.old # remove the temp file

将您的 favicon.ico 文件放在 ./static/ 目录

勾选这个https://beego.me/docs/quickstart/static.md