V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
chenqh
V2EX  ›  Go 编程语言

golang pongo2 function 的问题

  •  
  •   chenqh · 2021-06-20 15:27:05 +08:00 · 1124 次点击
    这是一个创建于 1013 天前的主题,其中的信息可能已经有所发展或是发生改变。

    html

    
                    <div class="layui-form-item">
                        {{form_label("登陆绑定 IP" ,label_cls)}}
                        <div class="layui-input-inline" data-name="{{optionSessionBindIp.Name}}" data-value="optionSessionBindIp.Value">
                            {{switch_option_render(optionSessionBindIp, fn_format("/a/admin/setting/update?name=%s",option.Name) )}}
    
                        </div>
                    </div><!-- 登录绑定 IP -->
    

    fn_format 的代码

    
    func GinContextExtend(c *gin.Context, dataList ...pongo2.Context) pongo2.Context {
    	// 可以把获取当前用户放在这里
    	d := pongo2.Context{
    		"fn_format": fmt.Sprintf,
    	}
    	user, err := service.GinGetUser(c)
    	if err == nil {
    		d["user"] = user
    		menuList, err := ReadMenuList(user.Role)
    		if err != nil {
    			panic(err)
    		}
    		menuListByte, err := json.Marshal(menuList)
    		if err != nil {
    			panic(err)
    		}
    		d["menuList"] = string(menuListByte)
    	}
    
    	var newList = []pongo2.Context{
    		d,
    	}
    	newList = append(newList, dataList...)
    
    	return ContextExtend(newList...)
    }
    

    现在运行报错

    
    2021/06/20 15:18:05 [Recovery] 2021/06/20 - 15:18:05 panic recovered:
    [Error (where: execution) in /home/vagrant/gcode/gadmin/templates/admin/setting.html | Line 26 Col 27 near 'switch_option_render'] [Error (where: execution) in /home/vagrant/gcode/gadmin/templates/admin/setting.html | Line 26 Col 69 near 'fn_format'] Calling a function using an invalid parameter
    /home/vagrant/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:914 (0x9f703b)
            (*Context).Render: panic(err)
    /home/vagrant/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:923 (0x9f70f8)
            (*Context).HTML: c.Render(code, instance)
    /home/vagrant/gcode/gadmin/router/page_router/page_admin_setting.go:45 (0xb0c3d3)
            PageAdminSetting.func1: c.HTML(200, "admin/setting.html", render.GinContextExtend(c, pongo2.Context{
    /home/vagrant/gcode/gadmin/router/page_router/page_admin_setting.go:54 (0xb0bcae)
            PageAdminSetting: }()
    /home/vagrant/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165 (0xada3e6)
            (*Context).Next: c.handlers[c.index](c)
    /home/vagrant/gcode/gadmin/pkg/middleware/middleware.go:73 (0xada3c5)
            LoginRequired.func1.1: c.Next()
    /home/vagrant/gcode/gadmin/pkg/middleware/middleware.go:77 (0xadaa89)
            LoginRequired.func1: }()
    /home/vagrant/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165 (0xa06f73)
            (*Context).Next: c.handlers[c.index](c)
    /home/vagrant/go/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:241 (0xa06f32)
            LoggerWithConfig.func1: c.Next()
    /home/vagrant/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165 (0xa07e99)
            (*Context).Next: c.handlers[c.index](c)
    /home/vagrant/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:99 (0xa07e80)
            CustomRecoveryWithWriter.func1: c.Next()
    /home/vagrant/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165 (0xa06f73)
            (*Context).Next: c.handlers[c.index](c)
    /home/vagrant/go/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:241 (0xa06f32)
            LoggerWithConfig.func1: c.Next()
    /home/vagrant/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165 (0x9fd429)
            (*Context).Next: c.handlers[c.index](c)
    /home/vagrant/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:489 (0x9fd40f)
            (*Engine).handleHTTPRequest: c.Next()
    /home/vagrant/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:445 (0x9fcefb)
            (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
    /usr/local/go/src/net/http/server.go:2887 (0x7ac6c2)
            serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
    /usr/local/go/src/net/http/server.go:1952 (0x7a7aec)
            (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
    /usr/local/go/src/runtime/asm_amd64.s:1371 (0x46e800)
            goexit: BYTE    $0x90   // NOP
    

    该怎么弄呀, 在 macro 里面不能调用 function?

    py 转来 golang 好不习惯呀

    2 条回复    2021-06-21 19:22:04 +08:00
    chenqh
        1
    chenqh  
    OP
       2021-06-20 15:39:06 +08:00
    我自己的问题, 我知道怎么解决了
    x940727
        2
    x940727  
       2021-06-21 19:22:04 +08:00
    go 的代码这样看就好丑啊……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5382 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 07:33 · PVG 15:33 · LAX 00:33 · JFK 03:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.