gpt4 book ai didi

file - 戈朗 : Parsing all templates which are in different directories?

转载 作者:IT王子 更新时间:2023-10-29 02:21:03 30 4
gpt4 key购买 nike

这是我的目录结构:

[root@abc]# ll
drwxr-xr-x. 2 root root 133 Mar 26 16:13 credit
drwxr-xr-x. 2 root root 132 Mar 26 16:17 form
-rw-r--r--. 1 root root 6003 Mar 27 19:30 main.go

var tmpl = template.Must(template.ParseGlob("form/*")) 解析 form 目录中的所有文件。如何解析 credit 目录文件?

var tmpl = template.Must(template.ParseGlob("form/*","credit/*"))

不起作用。

最佳答案

var tmpl = template.Must(template.ParseGlob("*/*"))

简单示例:

目录结构:

-rw-r--r--  1 User  staff  194 Mar 28 07:39 main.go
drwxr-xr-x 3 User staff 102 Mar 28 07:38 test1
drwxr-xr-x 3 User staff 102 Mar 28 07:38 test2

测试1的内容

-rw-r--r--  1 User  staff  0 Mar 28 07:38 template1.html

测试2的内容

-rw-r--r--  1 User  staff  0 Mar 28 07:38 template2.html

主.go

package main

import (
"fmt"
"html/template"
)

func main() {
var tmpl = template.Must(template.ParseGlob("*/*"))
fmt.Println(tmpl.DefinedTemplates())
}

输出:

; defined templates are: "template2.html", "template1.html"

template.ParseGlob 使用 filepath.Glob 进行通配。

// parseGlob is the implementation of the function and method ParseGlob. (Lines 461-474 of template.go)
func parseGlob(t *Template, pattern string) (*Template, error) {
if err := t.checkCanParse(); err != nil {
return nil, err
}
filenames, err := filepath.Glob(pattern) // <- right here
if err != nil {
return nil, err
}
if len(filenames) == 0 {
return nil, fmt.Errorf("html/template: pattern matches no files: %#q", pattern)
}
return parseFiles(t, filenames...)
}

所以它遵循同样的规则:

// Glob returns the names of all files matching pattern or nil
// if there is no matching file. The syntax of patterns is the same
// as in Match. The pattern may describe hierarchical names such as
// /usr/*/bin/ed (assuming the Separator is '/').
//
// Glob ignores file system errors such as I/O errors reading directories.
// The only possible returned error is ErrBadPattern, when pattern
// is malformed. (Lines 226-233 path/filepath/match.go)

Match的描述是:

// Match reports whether name matches the shell file name pattern.
// The pattern syntax is:
//
// pattern:
// { term }
// term:
// '*' matches any sequence of non-Separator characters
// '?' matches any single non-Separator character
// '[' [ '^' ] { character-range } ']'
// character class (must be non-empty)
// c matches character c (c != '*', '?', '\\', '[')
// '\\' c matches character c
//
// character-range:
// c matches character c (c != '\\', '-', ']')
// '\\' c matches character c
// lo '-' hi matches character c for lo <= c <= hi
//
// Match requires pattern to match all of name, not just a substring.
// The only possible returned error is ErrBadPattern, when pattern
// is malformed.
//
// On Windows, escaping is disabled. Instead, '\\' is treated as
// path separator.
//

关于file - 戈朗 : Parsing all templates which are in different directories?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49517158/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com