小编Dia*_*ian的帖子

如何在Go模板中替换字符串?

我用的是"text/template"模块.

我有像这样的结构来解析Blogger中的XML

type Media struct {
    ThumbnailUrl string `xml:"url,attr"`
}


type Entry struct {
    ID string `xml:"id"`
    Published Date `xml:"published"`
    Updated Date `xml:"updated"`
    Draft Draft `xml:"control>draft"`
    Title string `xml:"title"`
    Content string `xml:"content"`
    Tags Tags `xml:"category"`
    Author Author `xml:"author"`
    Media Media `xml:"thumbnail"`
    Extra string
}
Run Code Online (Sandbox Code Playgroud)

然后我像这样创建Go Template

[image]
    src = "{{ replace .Media.ThumbnailUrl 's72-c' 's1600' }}"
    link = ""
    thumblink = "{{ .Media.ThumbnailUrl }}"
    alt = ""
    title = ""
    author = ""
    license = ""
    licenseLink = "" …
Run Code Online (Sandbox Code Playgroud)

go go-templates

6
推荐指数
1
解决办法
6356
查看次数

标签 统计

go ×1

go-templates ×1