模板中的for循环 [英] for loop in templates

查看:85
本文介绍了模板中的for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要模板中的for循环.

for i := start; i < finish; i++ {
    // do something
}

range与准备好的数组一起使用只是一种方法,还是如何将此功能添加到模板中?

Is it just one way by using range with prepared array or how can I add this functional to templates?

推荐答案

最简单的方法可能是使用range和外部函数.例如(播放中):

The simplest way is probably to use range and an external function. For example (On play):

func For(start, end int) <-chan int {
    c := make(chan int)
    go func() {
        for i := start; i < end; i++ {
            c <- i
        }
        close(c)
    }()
    return c
}

在模板中:

{{range For 0 10}}
i: {{.}}
{{end}}

这篇关于模板中的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆