要处理Web应用程序中的每个请求,有关模板的常规代码如下所示:
t:= template.New("welcome")
t, _ = t.ParseFiles("welcome.tpl")
t.Execute(w, data)
Run Code Online (Sandbox Code Playgroud)
我想ParseFiles每次都要花很多钱.是否可以重用模板?所以我改进了这样:
//templateMap := make(map[string][template])
//...
tplName :="welcome"
t := templateMap[tplName]
if t=nil{
t:= template.New(tplName )
t, _ = t.ParseFiles("welcome.tpl")
templateMap[tplName] = t
}
t.Execute(w, data)
Run Code Online (Sandbox Code Playgroud)
我想通过将模板放入地图或缓存来提高效率是可行的还是可行的?我也想知道函数 Execute是否是线程安全的?
func (t *Template) Execute(wr io.Writer, data interface{}) (err error)
从http://code.google.com/p/leveldb/开始,似乎没有明确或容易将LevelDB与Go一起使用.但我真的想知道如何在Go中使用它.
谁能给我一个线索?
在java中,我们可以使用String的方法:byte [] getBytes(Charset charset).此方法使用给定的字符集将String编码为字节序列,并将结果存储到新的字节数组中.
但是如何在GO中做到这一点?Go中有没有类似的方法可以做到这一点?
请让我知道.
在这个方法中:
func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string
Run Code Online (Sandbox Code Playgroud)
第二个参数有什么作用?我试过了:
re, _ := regexp.Compile("a")
rs := re.FindAllString("aaaaa, ", **1**) // 1 get one 'a', 2 get two 'a's, 3 get three 'a's ...
for _,v := range rs {
fmt.Println(v)
}
Run Code Online (Sandbox Code Playgroud)
似乎第二个参数是关于它匹配的次数.我对吗?有人可以给我一个答案吗?官方文档或某些链接是首选.
我打算在log或diskqueue这样的系统中使用fdatasync.第一件事是在文件系统中创建一个带有"000000 ..."的10MB文件,如ext4.但我不知道如何正确地做到这一点.