微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 go 模板读取和解析文件

如何解决使用 go 模板读取和解析文件

上下文

我正在寻找一种很好的方法来循环 json 数组文件 以填充 Hugo 项目中的视频库。以下是我的大致想法:

content/
    some_section/
        index.md  (uses {{<iframe_gallery>}})
        videos.json
    ...

videos.json

[
    {
        "title": "some video","url": "https://www.youtube.com/embed/kjkdskdck"
    },{
        "title": "some other video","url": "https://www.youtube.com/embed/kjzflzjan"
    }
]

我发现了很多关于如何在 go 中加载和解析 json 文件的宝贵资源,例如 this one

问题

去和去模板对我来说很新鲜。我不明白如何转置我读到的关于用简单的去模板实现这一点的内容。我天真地开始

iframe_gallery.html

<section class="flex-ns flex-wrap justify-around center">
    {{ $jsonFile,$err := os.Open("./videos.json") }}

得到

Process: loading templates: "/home/zar3bski/Documents/Code/Sites/melainya/layouts/shortcodes/iframe_gallery.html:2:1": parse Failed: template: shortcodes/iframe_gallery.html:2: too many declarations in command

我觉得我遗漏了一个重要的区别,但未能以综合的方式理解它。你将如何加载、解析和循环这个 json 数组?有没有更直接的方法来实现这个目标?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。