-
Notifications
You must be signed in to change notification settings - Fork 0
/
golang-idioms.html
248 lines (210 loc) · 8.29 KB
/
golang-idioms.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Golang idioms — m0v</title>
<link rel="stylesheet" type="text/css" href="/theme/css/skeleton.css" />
<link rel="stylesheet" type="text/css" href="/theme/css/main.css" />
<link rel="shortcut icon" type="text/css" href="/favicon.ico" />
<link rel="alternate" type="application/atom+xml"
title="m0v — Flux Atom"
href="/" />
<!--[if lte IE 8]><script src="/theme/js/html5shiv.js"></script><![endif]-->
<meta name="viewport" content="initial-scale=1.0, width=device-width, user-scalable=no" />
<meta name="author" content="RCS" />
<meta name="keywords" content="go, software" />
</head>
<body>
<div class="page">
<header class="page-head">
<h1>
<a href="/index.html">m0v</a>
</h1>
</header>
<div class="page-content">
<article class="post content">
<header class="post-header">
<time datetime="2017-01-15T00:00:00+00:00" pubdate="pubdate">
Sun 15 January 2017
</time>
<h1>
<a rel="bookmark"
href="/golang-idioms.html"
title="Lien permanent vers «Golang idioms»">
Golang idioms
</a>
</h1>
<div class="meta">
In «<a href="/category/cheat-sheet.html">Cheat Sheet</a>»
by <a href="/author/rcs.html">RCS</a>
</div>
</header>
<div class="post-content">
<h2><a href="#contents" id="contents">Contents</a></h2>
<ul>
<li><a href="#funcy">Funcy interface</a></li>
<li><a href="#wrap">WrapperAdapter</a></li>
<li><a href="#limit">Limit goroutines</a></li>
<li><a href="#dispatch">Dynamic dispatch</a></li>
</ul>
<h3><a href="#funcy" id="funcy">Funcy interface</a></h3>
<ul>
<li>Single function interfaces can be supplanted by function types, the most popluar example being http.Handler & http.HandlerFunc <div class="highlight"><pre><span></span>type Handler interface {
ServeHTTP(w http.ResponseWriter, r *http.Request)
}
type HandlerFunc(w http.ResponseWriter, r *http.Request) func
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f(w, r)
}
</pre></div>
</li>
</ul>
<h3><a href="#wrap" id="wrap">Wrapper Adapter</a></h3>
<ul>
<li>Wrapping preserves interfaces while adding functionality <div class="highlight"><pre><span></span>func Wrap (h http.Handler) http.Handler {
return &wrapper{ handler: h}
}
type wrapper struct {
handler http.Handler
}
func (w* wrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Do something before
w.handler.ServeHTTP(w, r)
// Do something after
}
func main() {
myHandler := NewHandler();
http.Handle("/", Wrap(myHandler))
}
</pre></div>
</li>
</ul>
<h3><a href="#limit" id="limit">Limit goroutines</a></h3>
<ul>
<li>Use a buffered channel to limit creation of simultaneously active goroutines.<div class="highlight"><pre><span></span>var <span class="p">(</span>
concurrent <span class="o">=</span> <span class="m">5</span>
limit <span class="o">=</span> make<span class="p">(</span>chan struct<span class="p">{},</span> concurrent<span class="p">)</span>
<span class="p">)</span>
func doWork<span class="p">(</span>i int<span class="p">)</span> <span class="p">{</span>
limit <span class="o"><-</span> struct<span class="p">{}{}</span> <span class="o">//</span> block until slot is released by existing goroutine
go func <span class="p">{</span>
defer func<span class="p">()</span> <span class="p">{</span>
<span class="o"><-</span>limit <span class="o">//</span> release a slot
<span class="p">}</span>
log.Println<span class="p">(</span>i<span class="p">)</span>
time.Sleep<span class="p">(</span><span class="m">1</span><span class="o">*</span>time.Second<span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
func main<span class="p">()</span> <span class="p">{</span>
<span class="kr">for</span> i<span class="o">:=</span><span class="m">0</span><span class="p">;</span> i <span class="o"><</span> <span class="m">10000</span><span class="p">;</span> i<span class="o">++</span> <span class="p">{</span>
doWork<span class="p">(</span>i<span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</li>
</ul>
<h3><a href="#dispatch" id="dispatch">Dynamic dispatch</a></h3>
<ul>
<li>
<p>A combination of a single function interface and dynamic dispatch can be used for validation during entity creation / updation. For instance: </p>
</li>
<li>
<p>Let the entity do it's own validation by having it implement a Validator interface </p>
<div class="highlight"><pre><span></span>type Validator interface {
func Validate(v ...interface{}) error
}
type User struct {
Email string
}
// checks for errors in u optionally against v
func (u *User) Validate(v ...interface{}) error {
}
</pre></div>
</li>
<li>
<p>A function Validate can then select a Validator depending on the type of the interface.</p>
<div class="highlight"><pre><span></span>func Validate(u interface{}, v ...interface{}) error {
uu := reflect.ValueOf(u)
err := selectValidatorType(uu.Type())(uu, v...)
return err
}
var (
validatorType = reflect.TypeOf(new(Validator)).Elem()
)
type validatorFunc func(u reflect.Value, vv ...interface{}) error
func selectValidatorType(t reflect.Type) validatorFunc {
if t.Implements(validatorType) {
return selectValidator
} else {
return errorValidator
}
}
func selectValidator(u reflect.Value, v ...interface{}) error {
if u.Kind() == reflect.Ptr && u.IsNil() {
return DSErr{When: time.Now(), What: "Nil Value"}
}
m := u.Interface().(Validator)
err := m.Validate(v...)
if err != nil {
return DSErr{When: time.Now(), What: err.Error()}
}
return nil
}
func errorValidator(u reflect.Value, v ...interface{}) error {
return DSErr{When: time.Now(), What: "Not a Validator"}
}
</pre></div>
</li>
</ul>
</div>
<footer class="post-footer">
<div class="meta">
In «<a href="/category/cheat-sheet.html">Cheat Sheet</a>»
by <a href="/author/rcs.html">RCS</a><br />
Tags: #<a href="/tag/go.html">go</a> #<a href="/tag/software.html">software</a> </div>
</footer>
</article>
</div>
<aside class="page-sidebar">
<nav class="widget">
<h3>Site</h3>
<ul>
<li><a href="/index.html">Home</a></li>
<li><a href="/archives.html">Archives</a></li>
<li><a href="/tags.html">Tags</a></li>
</ul>
</nav>
<nav class="widget">
<h3>Categories</h3>
<ul>
<li><a href="/category/algorithms.html">Algorithms</a></li>
<li><a href="/category/chatter.html">Chatter</a></li>
<li><a href="/category/cheat-sheet.html">Cheat Sheet</a></li>
<li><a href="/category/cheatsheet.html">cheatsheet</a></li>
<li><a href="/category/detailed.html">Detailed</a></li>
<li><a href="/category/events.html">Events</a></li>
<li><a href="/category/language.html">language</a></li>
<li><a href="/category/manual.html">manual</a></li>
<li><a href="/category/misc.html">misc</a></li>
<li><a href="/category/plan.html">plan</a></li>
<li><a href="/category/review.html">Review</a></li>
<li><a href="/category/setup.html">Setup</a></li>
<li><a href="/category/software-encryption.html">software, encryption</a></li>
<li><a href="/category/teaser.html">Teaser</a></li>
</ul>
</nav>
<nav class="widget">
<h3>Social</h3>
<ul>
<li><a href="https://stackoverflow.com/users/1093710/sridhar">SO</a></li>
<li><a href="https://github.com/tortuoise">Github</a></li>
<li><a href="/" rel="alternate">Flux Atom</a></li>
</ul>
</nav>
</aside>
<footer class="page-footer">
<p> Powered by <a href="http://pelican.readthedocs.org">Pelican</a></p>
</footer>
</div>
</body>
</html>