跳到主要内容

模板项目

Github StackBlitz

本项目演示了如何设置一个 Go 应用程序,使用模板渲染、Tailwind CSS 和 Parcel 进行资源打包。

先决条件

请确保您已安装以下内容

  • Golang
  • Node.js
  • npm

设置

  1. 克隆仓库

    git clone https://github.com/gofiber/recipes.git
    cd recipes/template
  2. 安装依赖项

    npm install

使用方法

构建资源

  1. 构建资源

    npm run build
  2. 监听资源变化

    npm run dev

运行应用程序

  1. 启动 Fiber 应用程序
    go run main.go

示例

这里是一个如何在 Go 中使用模板渲染设置基本路由的示例

package main

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

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

// Create a new Fiber instance with the template engine
app := fiber.New(fiber.Config{
Views: engine,
})

// Define a route
app.Get("/", func(c *fiber.Ctx) error {
return c.Render("index", fiber.Map{
"Title": "Hello, World!",
})
})

// Start the server
app.Listen(":3000")
}

参考资料