首页 > 后端开发 > Golang > 正文

Go 语言中模板格式化函数与 HTTP 处理的实践

碧海醫心
发布: 2025-07-13 18:26:18
原创
577人浏览过

go 语言中模板格式化函数与 http 处理的实践

本文深入探讨了 Go 语言中 template.FormatterMap 的使用,解释了为何需要包装 template.HTMLEscape 函数以适应格式化映射的签名要求。同时,简要介绍了如何修改 HTTP 处理函数以接受命令行参数,从而实现更灵活的 Web 服务。

理解 template.FormatterMap 和格式化函数

在 Go 语言的 html/template 包中,FormatterMap 允许你自定义模板中特定标识符的格式化方式。它是一个字符串到函数的映射,其中字符串是模板中使用的标识符,函数则负责将数据格式化为字符串并写入 io.Writer。

FormatterMap 的类型定义如下:

type FormatterMap map[string]func(io.Writer, interface{}, string)
登录后复制

这意味着,映射中的每个函数必须接受三个参数:

  1. io.Writer: 用于写入格式化后的输出。
  2. interface{}: 要格式化的数据,可以是任何类型。
  3. string: 格式字符串,用于指定格式化选项。

template.HTMLEscape 函数的签名如下:

func HTMLEscape(w io.Writer, b []byte)
登录后复制

它只接受一个 io.Writer 和一个字节切片作为参数。因此,直接将 template.HTMLEscape 函数关联到 FormatterMap 中的键是不可能的,因为它们的签名不匹配。

UrlHtmlFormatter 的作用

UrlHtmlFormatter 函数充当了一个适配器,它将 template.HTMLEscape 函数的签名调整为 FormatterMap 所需的签名。

func UrlHtmlFormatter(w io.Writer, v interface{}, fmt string) {
    template.HTMLEscape(w, []byte(http.URLEscape(v.(string))))
}
登录后复制

在这个函数中:

  1. v.(string) 将 interface{} 类型的输入 v 断言为字符串类型。
  2. http.URLEscape(v.(string)) 对字符串进行 URL 编码。
  3. []byte(...) 将 URL 编码后的字符串转换为字节切片。
  4. template.HTMLEscape(w, ...) 使用 template.HTMLEscape 函数对字节切片进行 HTML 转义,并将结果写入 io.Writer。
  5. fmt string虽然定义了,但是没有实际作用,可以用于后续扩展

这样,UrlHtmlFormatter 函数就满足了 FormatterMap 的签名要求,同时利用 template.HTMLEscape 函数实现了 HTML 转义的功能。

示例代码

以下是使用 FormatterMap 和 UrlHtmlFormatter 的一个完整示例:

package main

import (
    "flag"
    "fmt"
    "html/template"
    "io"
    "log"
    "net/http"
    "net/url"
)

var addr = flag.String("addr", ":1718", "http service address")
var fmap = template.FuncMap{
    "html":     template.HTMLEscapeString,
    "url+html": UrlHtmlFormatter,
}
var templ = template.Must(template.New("qr").Funcs(fmap).Parse(templateStr))

func main() {
    flag.Parse()
    http.HandleFunc("/", QR)
    fmt.Printf("Starting server on %s\n", *addr)
    err := http.ListenAndServe(*addr, nil)
    if err != nil {
        log.Fatal("ListenAndServe:", err)
    }
}

func QR(w http.ResponseWriter, req *http.Request) {
    err := templ.Execute(w, req.URL.Query().Get("s"))
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

func UrlHtmlFormatter(w io.Writer, v interface{}, fmt string) (int, error) {
    escapedURL := url.QueryEscape(fmt.Sprintf("%v", v))
    _, err := io.WriteString(w, template.HTMLEscapeString(escapedURL))
    if err != nil {
        return 0, err
    }
    return 0, nil
}

const templateStr = `
<html>
<head>
<title>QR Link Generator</title>
</head>
<body>
@@##@@
<br>
{{.|html}}
<br>
<br>
<form action="/" method="GET">
    <input maxLength="1024" size="70" name="s" value="" title="Text to QR Encode">
    <input type="submit" value="Show QR" name="qr">
</form>
</body>
</html>
`
登录后复制

总结

通过使用 FormatterMap 和适配器函数,可以灵活地自定义模板的格式化行为。UrlHtmlFormatter 函数就是一个典型的例子,它将 template.HTMLEscape 函数适配到 FormatterMap 的签名要求,实现了 URL 编码和 HTML 转义的功能。理解这些概念对于构建复杂的 Go Web 应用至关重要。

Go 语言中模板格式化函数与 HTTP 处理的实践

以上就是Go 语言中模板格式化函数与 HTTP 处理的实践的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号