1 package main
2
3 import (
4 "golang.org/x/text/language"
5 "golang.org/x/text/message"
6 )
7
8
9
10
11 func main() {
12 p := message.NewPrinter(language.English)
13
14
15
16 wrapf(p, "inline %s", "ARG1")
17 gwrapf("global printer used %s", "ARG1")
18
19 w := wrapped{p}
20
21
22 w.wrapf("number: %d, string: %s, bool: %v", 2, "STRING ARG", true)
23 w.wrapf("empty string")
24 w.wrap("Lovely weather today!")
25
26 more(&w)
27 }
28
29 var printer = message.NewPrinter(language.English)
30
31 func more(w wrapper) {
32 w.wrap("number one")
33 w.wrapf("speed of light: %s", "C")
34 }
35
36 func gwrapf(format string, args ...interface{}) {
37 v := format
38 a := args
39 printer.Printf(v, a...)
40 }
41
42 func wrapf(p *message.Printer, format string, args ...interface{}) {
43 v := format
44 a := args
45 p.Printf(v, a...)
46 }
47
48 func wrap(p *message.Printer, format string) {
49 v := format
50 b := "0"
51 a := []interface{}{3, b}
52 s := a[:]
53 p.Printf(v, s...)
54 }
55
56 type wrapper interface {
57 wrapf(format string, args ...interface{})
58 wrap(msg string)
59 }
60
61 type wrapped struct {
62 p *message.Printer
63 }
64
65
66
67
68 func (w wrapped) wrapf(format string, args ...interface{}) {
69 w.p.Printf(format, args...)
70 }
71
72 func (w *wrapped) wrap(msg string) {
73 w.p.Printf(msg)
74 }
75
76 func fint(p *message.Printer, x int) {
77 v := "number: %d"
78 const c = "DAFDA"
79 p.Printf(v, c)
80 }
81
82 const format = "constant local" + " %s"
83
84
85 func pass(p *message.Printer, args ...interface{}) {
86
87
88 p.Sprintf(format, args...)
89 }
90
91 func lookup(p *message.Printer, x int) {
92
93 p.Printf(foo[x])
94 }
95
96 var foo = []string{
97 "aaaa",
98 "bbbb",
99 }
100
101 func field(p *message.Printer, x int) {
102
103
104 p.Printf(strct.Foo.Bar, x)
105 }
106
107 type fooStruct struct {
108 Foo barStruct
109 }
110
111 type barStruct struct {
112 other int
113 Bar string
114 }
115
116 var strct = fooStruct{
117 Foo: barStruct{0, "foo %d"},
118 }
119
120 func call(p *message.Printer, x int) {
121
122 p.Printf(fn())
123 }
124
125 func fn() string {
126 return "const str"
127 }
128
129
130 func ifConst(p *message.Printer, cond bool, arg1 string) {
131 a := "foo %s %s"
132 if cond {
133 a = "bar %s %s"
134 }
135 b := "FOO"
136 if cond {
137 b = "BAR"
138 }
139 wrapf(p, a, arg1, b)
140 }
141
142
143 func ifConst2(x int) {
144 a := ""
145 switch x {
146 case 0:
147 a = "foo"
148 case 1:
149 a = "bar"
150 case 2:
151 a = "baz"
152 }
153 gwrapf(a)
154 }
155
156
157 func freeVar(p *message.Printer, str string) {
158 fn := func(p *message.Printer) {
159 p.Printf(str)
160 }
161 fn(p)
162 }
163
164 func freeConst(p *message.Printer) {
165
166 const str = "const str"
167 fn := func(p *message.Printer) {
168 p.Printf(str)
169 }
170 fn(p)
171 }
172
173 func global(p *message.Printer) {
174
175 city := "Amsterdam"
176
177 p.Printf(globalStr, city)
178 }
179
180
181 var globalStr = "See you around in %s!"
182
183 func global2(p *message.Printer) {
184 const food = "Pastrami"
185 wrapf(p, constFood,
186 food,
187 )
188 }
189
190
191 var (
192
193 notAMessage, constFood, msgHello = "NOPE!", consume, hello
194 )
195
196
197 var (
198
199 hello = "Hello, %d and %s!"
200
201 consume = "Please eat your %s!"
202 )
203
View as plain text