You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu服务器Golang Fiber渲染模板报错:render: template index does not exist

问题:Fiber框架在Ubuntu服务器上无法找到HTML模板(本地Mac环境正常)

在DigitalOcean的Ubuntu 22.10服务器上测试Golang + Fiber框架 + HTML模板引擎时,本地Mac环境运行完全正常(MySQL连接、邮件发送功能均无问题),但服务器端持续报错:render: template index does not exist。

文件结构

├── /gogo
   ├── main
   ├── main.go
   ├── go.mod
   ├── go.sum
   ├── /views
        └── index.html
   └── /public
        └── plaatje.png

main.go 代码

package main

import (
    "fmt"
    "log"
    fiber "github.com/gofiber/fiber/v2"
    "github.com/gofiber/template/html"
)

func main() {
    // Initialize standard Go html template engine
    template_engine := html.New(
        "./views",
        ".html",
    )

    // start fiber
    app := fiber.New(fiber.Config{
        Views: template_engine,
    })

    // add static folder
    app.Static(
        "/static",  // mount address
        "./public", // path to the file folder
    )

    // endpoint
    app.Get("/", func(c *fiber.Ctx) error {
        // Render index template
        return c.Render("index", fiber.Map{
            "Title": "It works",
            "Plat":  "almost",
        })
    })

    log.Fatal(app.Listen(":9990"))
}

index.html 文件内容

<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=Unicode">
    <title>{{.Title}}</title>
</head>
<body>
    <h1>{{.Title}}</h1>
    <p>{{.Plat}}</p>
    <p><img src="./static/plaatje.png"></p>
</body>
</html>

已尝试的解决方法

  • 修改Ubuntu服务器上文件的所有权和权限
  • 调整views路径(尝试过./views、/views、views等多种写法)
  • 将c.Render的第一个参数改为"index.html"

以上操作均未解决问题,请问我还遗漏了什么?

内容的提问来源于stack exchange,提问作者Jeex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 01:25:21