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
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Home for a73x" />
<meta name="author" content="a73x" />
<meta name="viewport"
content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, width=device-width" />
<title>a73x</title>
<link rel="stylesheet" href="/static/styles.css">
<link rel="stylesheet" href="/static/syntax.css">
<link rel="icon" type="image/x-icon" href="/static/favicon.svg">
</head>
<body>
<main>
<div class="header">
<div class="header-title">
<h1>a73x</h1>
<sub>high effort, low reward</sub>
</div>
<nav class="nav">
<ul>
<li><a class="no-decorations" href="/">Home</a></li>
<li><a class="no-decorations" href="/posts">Posts</a></li>
<li><a class="no-decorations" href="/ethos">Ethos</a></li>
</ul>
</nav>
</div>
<hr />
<a href="/posts">← Posts</a>
<h1>Simplifying Interfaces with Function Types</h1>
<h1 id="table-of-contents">Table of Contents</h1>
<ul>
<li>
<a href="#example">Example</a></li>
<li>
<a href="#how-it-works">How it works</a></li>
<li>
<a href="#application">Application</a></li>
</ul>
<p>In Go, you can define methods on type aliases, which means that we can define a type alias of a function, and then define methods on that function.</p>
<h2 id="example">Example</h2>
<p>Given the following interface</p>
<pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;white-space:pre-wrap;word-break:break-word;"><code><span style="color:#00a">type</span> DB <span style="color:#00a">interface</span> {
<span style="color:#0a0">Get</span>(<span style="color:#0aa">string</span>) (<span style="color:#0aa">string</span>, <span style="color:#0aa">error</span>)
}
</code></pre><p>You can fulfill it using a function type like this:</p>
<pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;white-space:pre-wrap;word-break:break-word;"><code><span style="color:#00a">type</span> GetFn <span style="color:#00a">func</span>(<span style="color:#0aa">string</span>) (<span style="color:#0aa">string</span>, <span style="color:#0aa">error</span>)
<span style="color:#00a">func</span> (f GetFn) <span style="color:#0a0">Get</span>(a <span style="color:#0aa">string</span>) (<span style="color:#0aa">string</span>, <span style="color:#0aa">error</span>) {
<span style="color:#00a">return</span> <span style="color:#0a0">f</span>(a)
}
</code></pre><p>Now you can use GetFn whenever a DB is required:</p>
<pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;white-space:pre-wrap;word-break:break-word;"><code><span style="color:#00a">func</span> <span style="color:#0a0">main</span>() {
<span style="color:#00a">var</span> storeFn DB = <span style="color:#0a0">GetFn</span>(<span style="color:#00a">func</span>(s <span style="color:#0aa">string</span>) (<span style="color:#0aa">string</span>, <span style="color:#0aa">error</span>) {
<span style="color:#00a">return</span> <span style="color:#a50">"bar"</span>, <span style="color:#00a">nil</span>
})
fmt.<span style="color:#0a0">Println</span>(storeFn.<span style="color:#0a0">Get</span>(<span style="color:#a50">"Foo"</span>)) <span style="color:#aaa;font-style:italic">// Outputs: bar
</span><span style="color:#aaa;font-style:italic"></span>}
</code></pre><p>You can try this example in this [Go Playground](<a href="https://go.dev/play/p/hyBNIMblafs">https://go.dev/play/p/hyBNIMblafs</a></p>
<h2 id="how-it-works">How it works</h2>
<p>In Go, interfaces are implicitly through method sets, which means any type (including a function type) that defines the required methods satisfies the interface. By defining the <code>Get</code> method on the <code>GetFn</code> type, the compiler treats <code>GetFn</code> as a valid implementation of the DB interface.</p>
<p>This flexibility allows you to use function types as lightweight, dynamic implementations of interfaces, without the need for full struct definitions.</p>
<h2 id="application">Application</h2>
<p>One common use case for this pattern is testing. Instead of implementing a full mock, you can use an inline function to provide test specific behavior.</p>
<pre tabindex="0" style="background-color:#f0f0f0;-moz-tab-size:4;-o-tab-size:4;tab-size:4;white-space:pre-wrap;word-break:break-word;"><code><span style="color:#00a">func</span> <span style="color:#0a0">TestHandler</span>() {
mockDB := <span style="color:#0a0">GetFn</span>(<span style="color:#00a">func</span>(key <span style="color:#0aa">string</span>) (<span style="color:#0aa">string</span>, <span style="color:#0aa">error</span>) {
<span style="color:#00a">if</span> key == <span style="color:#a50">"foo"</span> {
<span style="color:#00a">return</span> <span style="color:#a50">"bar"</span>, <span style="color:#00a">nil</span>
}
<span style="color:#00a">return</span> <span style="color:#a50">""</span>, fmt.<span style="color:#0a0">Errorf</span>(<span style="color:#a50">"not found"</span>)
})
result, err := mockDB.<span style="color:#0a0">Get</span>(<span style="color:#a50">"foo"</span>)
fmt.<span style="color:#0a0">Println</span>(result, err) <span style="color:#aaa;font-style:italic">// Outputs: bar, <nil>
</span><span style="color:#aaa;font-style:italic"></span>}
</code></pre><p>This approach is not limited to testing. It’s also useful for dependency injection, where you can pass in lightweight or context specific implementations of an interface.</p>
<p>This pattern is similar to how <code>http.HandleFunc</code> works. In the HTTP package, <code>http.HandlerFunc</code> is a function type that fulfills the <code>http.Handler</code> interface by implementing its <code>ServeHTTP</code> method. This allows functions to act as handlers, providing great flexibility in designing web servers.</p>
<footer>
<br />
<hr />
<br />
<p>see something you disagree with? email: <a href="mailto:[email protected]">[email protected]</a></p>
</footer>
</main>
</body>
</html>
|