• [golang] call the struct function in the template range


    here is the code of a struct

    type Post struct {
        Id          int    `db:"id"`
        Title       string `db:"title"`
        Content     string `db:"content"`
        Create_time int64    `db:"create_time"`
    }
    
    func (p Post) AttrCreatetime() time.Time {
        return time.Unix(p.Create_time, 0)
    }
     

    How to call the function "AttrCreatetime" in the template range? 

    That is very simple to achieve it. Here is the code of the html file.

                            {{range .posts}}
                <article class="article ar-in">
                    <div class="ar">
                        <span class="ar-ti">
                            <a href="{{url "Post.View" .Id}}" title="{{.Title}}">{{.Title}}</a>
                        </span>
                        <small>{{.AttrCreatetime}}</small>
                    </div>
                    <div class="ar-ds">
                        {{.Content}}
                        <span class="ar-mr"><a href="{{url "Post.View" .Id}}">more...</a></span>
                    </div>
                </article>
                {{end}}

    See? {{.AttrCreatetime}} is now la!

    Have fun with golang!

  • 相关阅读:
    线程池原理和实现
    线程
    ajax、xstream、json
    上传、下载、javamail
    过滤器
    监听器、国际化
    jsp、javabean、el
    jstl、标签
    jsp、cookie、httpsession
    个人觉得比较好用的chrome插件
  • 原文地址:https://www.cnblogs.com/davidhhuan/p/3042151.html
Copyright © 2020-2023  润新知