mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:33:04 +08:00
add chapter 15.7 (#683)
This commit is contained in:
18
eBook/examples/chapter_15/pipeline1.go
Normal file
18
eBook/examples/chapter_15/pipeline1.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// pipeline1.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t := template.New("template test")
|
||||
t = template.Must(t.Parse("This is just static text. \n{{\"This is pipeline data - because it is evaluated within the double braces.\"}} {{`So is this, but within reverse quotes.`}}\n"))
|
||||
t.Execute(os.Stdout, nil)
|
||||
}
|
||||
|
||||
/*
|
||||
This is just static text.
|
||||
This is pipeline data - because it is evaluated within the double braces. So is this, but within reverse quotes.
|
||||
*/
|
15
eBook/examples/chapter_15/predefined_functions.go
Normal file
15
eBook/examples/chapter_15/predefined_functions.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// predefined_functions.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t := template.New("test")
|
||||
t = template.Must(t.Parse("{{with $x := `hello`}}{{printf `%s %s` $x `Mary`}}{{end}}!\n"))
|
||||
t.Execute(os.Stdout, nil)
|
||||
}
|
||||
|
||||
// hello Mary!
|
24
eBook/examples/chapter_15/template_field.go
Normal file
24
eBook/examples/chapter_15/template_field.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// template_field.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
Name string
|
||||
nonExportedAgeField string
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := template.New("hello")
|
||||
t, _ = t.Parse("hello {{.Name}}!")
|
||||
p := Person{Name: "Mary", nonExportedAgeField: "31"}
|
||||
if err := t.Execute(os.Stdout, p); err != nil {
|
||||
fmt.Println("There was an error:", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Output: hello Mary!
|
27
eBook/examples/chapter_15/template_ifelse.go
Normal file
27
eBook/examples/chapter_15/template_ifelse.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// template_ifelse.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tEmpty := template.New("template test")
|
||||
tEmpty = template.Must(tEmpty.Parse("Empty pipeline if demo: {{if ``}} Will not print. {{end}}\n")) //empty pipeline following if
|
||||
tEmpty.Execute(os.Stdout, nil)
|
||||
|
||||
tWithValue := template.New("template test")
|
||||
tWithValue = template.Must(tWithValue.Parse("Non empty pipeline if demo: {{if `anything`}} Will print. {{end}}\n")) //non empty pipeline following if condition
|
||||
tWithValue.Execute(os.Stdout, nil)
|
||||
|
||||
tIfElse := template.New("template test")
|
||||
tIfElse = template.Must(tIfElse.Parse("if-else demo: {{if `anything`}} Print IF part. {{else}} Print ELSE part.{{end}}\n")) //non empty pipeline following if condition
|
||||
tIfElse.Execute(os.Stdout, nil)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Empty pipeline if demo:
|
||||
Non empty pipeline if demo: Will print.
|
||||
if-else demo: Print IF part.
|
||||
*/
|
23
eBook/examples/chapter_15/template_validation.go
Normal file
23
eBook/examples/chapter_15/template_validation.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// template_validation.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"text/template"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tOk := template.New("ok")
|
||||
//a valid template, so no panic with Must:
|
||||
template.Must(tOk.Parse("/* and a comment */ some static text: {{ .Name }}"))
|
||||
fmt.Println("The first one parsed OK.")
|
||||
fmt.Println("The next one ought to fail.")
|
||||
tErr := template.New("error_template")
|
||||
template.Must(tErr.Parse(" some static text {{ .Name }"))
|
||||
}
|
||||
|
||||
/* Output:
|
||||
The first one parsed OK.
|
||||
The next one ought to fail.
|
||||
panic: template: error_template:1: unexpected "}" in command
|
||||
*/
|
25
eBook/examples/chapter_15/template_variables.go
Normal file
25
eBook/examples/chapter_15/template_variables.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// template_variables.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t := template.New("test")
|
||||
t = template.Must(t.Parse("{{with $3 := `hello`}}{{$3}}{{end}}!\n"))
|
||||
t.Execute(os.Stdout, nil)
|
||||
|
||||
t = template.Must(t.Parse("{{with $x3 := `hola`}}{{$x3}}{{end}}!\n"))
|
||||
t.Execute(os.Stdout, nil)
|
||||
|
||||
t = template.Must(t.Parse("{{with $x_1 := `hey`}}{{$x_1}} {{.}} {{$x_1}}{{end}}!\n"))
|
||||
t.Execute(os.Stdout, nil)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
hello!
|
||||
hola!
|
||||
hey hey hey!
|
||||
*/
|
21
eBook/examples/chapter_15/template_with_end.go
Normal file
21
eBook/examples/chapter_15/template_with_end.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// template_with_end.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t := template.New("test")
|
||||
t, _ = t.Parse("{{with `hello`}}{{.}}{{end}}!\n")
|
||||
t.Execute(os.Stdout, nil)
|
||||
|
||||
t, _ = t.Parse("{{with `hello`}}{{.}} {{with `Mary`}}{{.}}{{end}}{{end}}!\n")
|
||||
t.Execute(os.Stdout, nil)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
hello!
|
||||
hello Mary!
|
||||
*/
|
Reference in New Issue
Block a user