duanci1858 2013-01-04 04:14
浏览 146

模板中带引号的字符串

I want to put in my html template (actually JavaScript part) null or string with email:

var email = null;
// or a string:
var email = "somebody@somewhere.com";

But with template

var email = {{.Email}};

I get quoted string everytime:

var email = "null";
var email = "somebody@somewhere.com";

How to fix it?

EDIT: here is my code: http://play.golang.org/p/8k4s8dv2PE

You can see Go surrounds string with quotes and removes comment - there is pre or post processing.

  • 写回答

2条回答 默认 最新

  • doumao9363 2013-01-04 06:45
    关注

    Without seeing your code, the best I can do is provide an example that shows your required functionality.

    If I had to take a guess, I'd say the data you were passing in was a string, not a string pointer. A string cannot have a value of nil/null, only a string pointer can. Though that would yield a "", not a "null", (as demonstrated here), so I can only imagine that this is something weird you're doing in your code.

    To prevent link-rot, I've duplicated the code examples below, though it's worth clicking through the links to see their output.

    Required Functionality:

    package main
    
    import (
        "log"
        "os"
        "html/template"
    )
    
    type TemplateData struct {
        Email *string
    }
    
    func main() {
    
        const temp = "<script>var email = {{.Email}};</script>
    "
    
        t := template.Must(template.New("email_template").Parse(temp))
        email := "somebody@somewhere.com"
        err := t.Execute(os.Stdout, TemplateData{
            Email: &email,
        })
        if err != nil {
            log.Println("executing template:", err)
        }
        err = t.Execute(os.Stdout, TemplateData{
            Email: nil,
        })
        if err != nil {
            log.Println("executing template:", err)
        }
    
    }
    

    Trying to use a "null" string:

    package main
    
    import (
        "log"
        "os"
        "html/template"
    )
    
    type TemplateData struct {
        Email string
    }
    
    func main() {
    
        const temp = "<script>var email = {{.Email}};</script>
    "
    
        t := template.Must(template.New("email_template").Parse(temp))
        email := "somebody@somewhere.com"
        err := t.Execute(os.Stdout, TemplateData{
            Email: email,
        })
        if err != nil {
            log.Println("executing template:", err)
        }
        err = t.Execute(os.Stdout, TemplateData{
        })
        if err != nil {
            log.Println("executing template:", err)
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?