From 8768ab6cad86995f9c124570919d3317a129ac44 Mon Sep 17 00:00:00 2001 From: devlights Date: Mon, 8 Sep 2025 18:11:40 +0900 Subject: [PATCH] Add txttmpl/missingkey.go --- examples/basic/templates/README.md | 1 + examples/basic/templates/txttmpl/examples.go | 1 + .../basic/templates/txttmpl/missingkey.go | 66 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 examples/basic/templates/txttmpl/missingkey.go diff --git a/examples/basic/templates/README.md b/examples/basic/templates/README.md index 8539e805..a4febf43 100644 --- a/examples/basic/templates/README.md +++ b/examples/basic/templates/README.md @@ -43,3 +43,4 @@ | txttmpl/parsefuncs/parsefiles.go | templates_parsefuncs_parsefiles | template.ParseFiles() のサンプルです. | | txttmpl/parsefuncs/parseglob.go | templates_parsefuncs_parseglob | template.ParseGlob() のサンプルです. | | htmltmpl/escape.go | templates_html_tmpl_escape | html/template にて適用されるHTMLエスケープについてのサンプルです. | +| txttmpl/missingkey.go | templates_text_tmpl_missingkey | text/template の missingkey=zero オプション指定時のサンプルです。 | diff --git a/examples/basic/templates/txttmpl/examples.go b/examples/basic/templates/txttmpl/examples.go index c658a7ad..f6aae252 100644 --- a/examples/basic/templates/txttmpl/examples.go +++ b/examples/basic/templates/txttmpl/examples.go @@ -51,6 +51,7 @@ func (r *register) Regist(m mapping.ExampleMapping) { m["templates_text_tmpl_with"] = With m["templates_text_tmpl_define"] = Define m["templates_text_tmpl_funcmap"] = FuncMap + m["templates_text_tmpl_missingkey"] = MissingKey parsefuncs.NewRegister().Regist(m) } diff --git a/examples/basic/templates/txttmpl/missingkey.go b/examples/basic/templates/txttmpl/missingkey.go new file mode 100644 index 00000000..de612cfa --- /dev/null +++ b/examples/basic/templates/txttmpl/missingkey.go @@ -0,0 +1,66 @@ +package txttmpl + +import ( + "fmt" + "os" + "text/template" +) + +// MissingKey -- text/template の missingkey=zero オプション指定時のサンプルです。 +// +// Template.Option("missingkey=zero")の設定を行い、テンプレートに対して map[string]any をデータとして指定した場合 +// キーに対応する値が nil の場合、"" と表示されることに注意。(マップの値の方の方がanyのため) +// +// 当然、any以外の場合はその型のゼロ値となる。 +// +// # REFERENCES +// - https://pkg.go.dev/text/template@latest +func MissingKey() error { + const ( + TEXT = `{{.Value}} world` + ) + var ( + mAny = map[string]any{"Value": "hello"} + mStr = map[string]string{"Value": "hello"} + mInt = map[string]int{"Value": 100} + + t = template.Must(template.New("MissingKey").Option("missingkey=zero").Parse(TEXT)) + fn = func(message string) { + fmt.Printf("[%s] -----------------------\n", message) + t.Execute(os.Stdout, mAny) + fmt.Println("") + t.Execute(os.Stdout, mStr) + fmt.Println("") + t.Execute(os.Stdout, mInt) + fmt.Println("") + } + ) + fn("キーあり") + + delete(mAny, "Value") + delete(mStr, "Value") + delete(mInt, "Value") + + fn("キーなし") + + return nil + + /* + $ task + task: [run] ./try-golang -onetime + + ENTER EXAMPLE NAME: templates_text_tmpl_missingkey + + [Name] "templates_text_tmpl_missingkey" + [キーあり] ----------------------- + hello world + hello world + 100 world + [キーなし] ----------------------- + world + world + 0 world + + [Elapsed] 79.082µs + */ +}