Go:模板中的引号字符串 [英] Go: quoted string in templates

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

问题描述

我想把我的html模板(实际上是JavaScript的一部分)放在null或带有email的字符串中:

  var email = null ; 
//或字符串:
var email =somebody@somewhere.com;

但带模板

  var email = {{.Email}}; 

每次获得带引号的字符串:

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

如何解决它?

编辑:这是我的代码: http://play.golang.org/p/8k4s8dv2PE



你可以看到Go用引号将字符串括起来,并删除注释 - 存在预处理或后处理。

解决方案

如果没有看到您的代码,我可以做的最好的办法是提供一个示例,显示您的必需功能

如果我不得不猜测,我会说您传入的数据是一个字符串,而不是字符串指针。一个字符串的值不能是nil / null,只能是一个字符串指针。虽然这会产生一个,而不是空(这里演示的),所以我只能想象这是你在代码中做的奇怪事情。



为了防止出现link-rot,我重复了下面的代码示例,尽管它值得点击链接查看输出结果。

必需的功能

<$ p $ (
log
os
html / template
$ b)
$ b $
$ b type TemplateData struct {
Email * string
}

func main(){

const temp =< script> ; var email = {{.Email}};< / script> \\\

$ b $: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(execu ting template:,err)
}
err = t.Execute(os.Stdout,TemplateData {
Email:nil,
})
if err!=无{
log.Println(执行模板:,错误)
}

}

尝试使用空字符串

 包主要

导入(
log
os
html / template


类型TemplateData struct {
电子邮件字符串
}

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(execution template:,err)
}
err = t.Execute(os.Stdout,TemplateData {
})
if err!= nil {
log.Println(正在执行的模板:,err)
}

}


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.

解决方案

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>\n"

    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>\n"

    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)
    }

}

这篇关于Go:模板中的引号字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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