我是靠谱客的博主 清脆灰狼,这篇文章主要介绍golang http 返回html文件_golang-gin(八)文件与HTML相关操作,现在分享给大家,希望可以做个参考。

b01adff875219bb646ffbec992f961c5.png

Serving static files(提供静态文件)

复制代码
1
2
3
4
5
6
7
8
9
10
func main() { router := gin.Default() router.Static("/assets", "./assets") router.StaticFS("/more_static", http.Dir("my_file_system")) router.StaticFile("/favicon.ico", "./resources/favicon.ico") // Listen and serve on 0.0.0.0:8080 router.Run(":8080") }

Serving data from file(从文件中提供数据)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
func main() { router := gin.Default() router.GET("/local/file", func(c *gin.Context) { c.File("local/file.go") }) var fs http.FileSystem = // ... router.GET("/fs/file", func(c *gin.Context) { c.FileFromFS("fs/file.go", fs) }) }

Serving data from reader(从读取器提供数据)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func main() { router := gin.Default() router.GET("/someDataFromReader", func(c *gin.Context) { response, err := http.Get("https://raw.githubusercontent.com/gin-gonic/logo/master/color.png") if err != nil || response.StatusCode != http.StatusOK { c.Status(http.StatusServiceUnavailable) return } reader := response.Body defer reader.Close() contentLength := response.ContentLength contentType := response.Header.Get("Content-Type") extraHeaders := map[string]string{ "Content-Disposition": `attachment; filename="gopher.png"`, } c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders) }) router.Run(":8080") }

HTML rendering(HTML渲染)

使用 LoadHTMLGlob() 或 LoadHTMLFiles()

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
func main() { router := gin.Default() router.LoadHTMLGlob("templates/*") //router.LoadHTMLFiles("templates/template1.html", "templates/template2.html") router.GET("/index", func(c *gin.Context) { c.HTML(http.StatusOK, "index.tmpl", gin.H{ "title": "Main website", }) }) router.Run(":8080") }

templates/index.tmpl

复制代码
1
2
3
4
5
<html> <h1> {{ .title }} </h1> </html>

在不同目录中使用同名模版

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func main() { router := gin.Default() router.LoadHTMLGlob("templates/**/*") router.GET("/posts/index", func(c *gin.Context) { c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{ "title": "Posts", }) }) router.GET("/users/index", func(c *gin.Context) { c.HTML(http.StatusOK, "users/index.tmpl", gin.H{ "title": "Users", }) }) router.Run(":8080") }

templates/posts/index.tmpl

复制代码
1
2
3
4
5
6
7
{{ define "posts/index.tmpl" }} <html><h1> {{ .title }} </h1> <p>Using posts/index.tmpl</p> </html> {{ end }}

templates/users/index.tmpl

复制代码
1
2
3
4
5
6
7
{{ define "users/index.tmpl" }} <html><h1> {{ .title }} </h1> <p>Using users/index.tmpl</p> </html> {{ end }}

Custom Template renderer(自定义模板渲染器)

您也可以使用自己的html模板渲染

复制代码
1
2
3
4
5
6
7
8
9
import "html/template" func main() { router := gin.Default() html := template.Must(template.ParseFiles("file1", "file2")) router.SetHTMLTemplate(html) router.Run(":8080") }

Custom Delimiters(自定义分隔符)

您可以使用自定义 delims

复制代码
1
2
3
4
r := gin.Default() r.Delims("{[{", "}]}") r.LoadHTMLGlob("/path/to/templates")

Custom Template Funcs(自定义模板功能)

main.go

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import ( "fmt" "html/template" "net/http" "time" "github.com/gin-gonic/gin" ) func formatAsDate(t time.Time) string { year, month, day := t.Date() return fmt.Sprintf("%d%02d/%02d", year, month, day) } func main() { router := gin.Default() router.Delims("{[{", "}]}") router.SetFuncMap(template.FuncMap{ "formatAsDate": formatAsDate, }) router.LoadHTMLFiles("./testdata/template/raw.tmpl") router.GET("/raw", func(c *gin.Context) { c.HTML(http.StatusOK, "raw.tmpl", gin.H{ "now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC), }) }) router.Run(":8080") }

raw.tmpl

复制代码
1
Date: {[{.now | formatAsDate}]}

Result:

复制代码
1
Date: 2017/07/01

最后

以上就是清脆灰狼最近收集整理的关于golang http 返回html文件_golang-gin(八)文件与HTML相关操作的全部内容,更多相关golang内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部