-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotated-go.html
249 lines (245 loc) · 8.45 KB
/
annotated-go.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
249
<!DOCTYPE html>
<html>
<head>
<title>The Annotated Go Program</title>
<meta charset="UTF-8">
<link rel="shortcut icon" type="image/png" href="http://www.353solutions.com/favicon.ico">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<style>
.keyword {
color: blue;
}
.string {
color: brown;
}
code {
font-family: 'Roboto Mono', monospace;
font-weight: bold;
}
.font {
font-size: 1.2em;
}
.tip:hover {
background: silver;
}
.marked {
background: silver;
}
p.help {
margin-top: 20px;
}
.footer {
position: absolute;
bottom: 0;
width: 100%;
/* Set the fixed height of the footer here */
height: 40px;
line-height: 40px; /* Vertically center the text there */
background-color: silver;
}
.right {
text-align: right;
}
img.mid-footer {
height: 20px;
}
img.gopher {
height: 15px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col col-10">
<h1>Annotated Go Program</h1>
</div>
<div class="col float-right">
<br />
<i id="help-btn" class="far fa-question-circle"></i> |
<input type="checkbox" id="highlight"></input> Highlight all
| <a target="_blank" href="https://golang.org"><img class="gopher" src="https://golang.org/favicon.ico"></img></a>
</div>
</div>
<div class="row">
<div class="col">
<hr/>
</div>
</div>
<div class="row">
<div class="col">
<pre><code class="font">
<span class="keyword tip" id="pkg">package</span> <span class="tip" id="main">main</span>
<span class="keyword tip" id="imp">import</span> (
<span class="string tip" id="fmt">"fmt"</span>
)
<span class="keyword tip" id="func">func</span> <span class="tip" id="mainfn">main</span>() {
<span class="tip" id="pkg-use">fmt</span>.<span class="tip" id="println">Println</span>(<span class="string tip" id="string">"Hello Gophers ☺"</span>)<span class="tip" id="semicolon"> </span>
}
</code></pre>
</div>
<div class="col">
<p id="help" class="font help">
</p>
</div>
</div>
<div class="row">
<div class="col">
<hr />
<code><pre class="font">
$ <span class="tip" id="go">go</span> <span class="tip" id="run">run</span> <span class="tip" id="ext">hw.go</span>
Hello Gophers!
</pre></code>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<div class="row">
<div class="col">
Created by
<a target="_blank" href="https://www.353solutions.com/">
<img src="https://www.353solutions.com/static/images/logo-dark.png" class="mid-footer"></img>
</a>
</div>
<div class="col right">
Licensed under <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>
</div>
</div>
</div>
</footer>
</body>
<script>
$(function() {
$('.tip').click(function() {
var id = $(this).attr('id');
$('#help').html('<p>' + doc[id] + '</p>');
$('a').attr('target', '_blank');
});
$('#help-btn').click(function(){
$('#help').html(initial_help);
});
$('#highlight').change(function() {
if ($('#highlight').prop('checked')) {
$('.tip').addClass('marked');
} else {
$('.tip').removeClass('marked');
}
});
$('#help-btn').click();
});
var doc = {
'pkg':`
Go is designed to enable working in large teams.
Every piece of code is in a package.
Other packages can only access exported names, these are names that start with
a capital letter.
`,
'main':`
<code>main</code> is a special package. This is the package the Go runtime runs
on startup.
<br /><br />
Read <a href="https://golang.org/doc/effective_go.html#package-names">here</a> about how to name your packages.
`,
'imp':`
You can import packages using the <code>import</code> keyword. Depnding you
your Go version, packages are either located in the <code>GOPATH</code>
environment variable or by the go
<a href="https://github.com/golang/go/wiki/Modules">modules</a>.
`,
'fmt': `
<a href="https://golang.org/pkg/fmt/">fmt</a> is a packge for formatting text.
Once you import a package, you can access public names within it (those
starting with a capital letter). If a path is composed of several parts (e.g.
<code>net/http</code>) you should use only the last part of the path (e.g.
<code>http.HandleFunc</code>).
<br />
You can rename a package by adding a name before the package to
be imported (e.g. <code>import httplib "net/http"</code>).
<br />
Read more about import paths <a
href="https://golang.org/doc/code.html#ImportPaths">here</a>.
`,
'func':`
Use the <code>func</code> keyword to define functions. Functions in Go can have
several arguments and return sevarl argument.<br/>
Usually the last argument returned by a function is an error code.<br/>
Read more about functions <a href="https://golang.org/doc/effective_go.html#functions">here</a>.
`,
'mainfn':`
<code>main.main</code> is a special function that the Go runtime runs on program start. There can be only one <code>main.main</code> function in a Go program.
`,
'pkg-use':`
To access function from a package, you need to prefix the function with the
last part of the package name.
`,
'println':`
<code>Println</code> will print it's message and then a new line. There are
several other print functions in the
<a href="https://golang.org/pkg/fmt/">fmt</a> package.
The ones starting with <code>F</code>
(e.g. <a href="https://golang.org/pkg/fmt/#Fprintln">Fprintln</a>) will print
to an <a href="https://golang.org/pkg/io/#Writer">io.Writer</a> and the ones
ending with <code>f</code> (e.g. <a
href="https://golang.org/pkg/fmt/#Printf">Printf</a>) support formatted
printing.
<br /><br />
<code>Println</code> is an exported function. Exported functions (and other
names) start with an upper case.
`,
'go':`
The <a href="https://golang.org/cmd/go/">go</a> tool is used to most common
tasks in Go -
<a href="https://golang.org/cmd/go/#hdr-Compile_packages_and_dependencies">building</a>,
<a href="https://golang.org/cmd/go/#hdr-Test_packages">testing</a>,
<a href="https://golang.org/cmd/go/#hdr-Download_and_install_packages_and_dependencies">installing dependencies</a> and more.
The <code>go</code> command is usually used from the command line but many IDEs
(such as <a href="https://code.visualstudio.com/">VSCode</a>
and <a href="https://www.jetbrains.com/go/">GoLand</a>) integrate it.
`,
'run':`
<a href="https://golang.org/cmd/go/#hdr-Compile_and_run_Go_program">go run</a>
compiles and runs an executable in one command. It allows for fast development
cycles. There is no official Go
<a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a> (or interactive prompt), but <code>go run</code> is about as effective.
`,
'string': `
Go strings support unicode and are encoded in <a
href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a> encoding.
<br /><br />
Strings in Go are immutable, you can use
<a href="https://golang.org/pkg/fmt/#Sprintf">fmt.Sprintf</a>,
<a href="https://golang.org/pkg/bytes/#Buffer">bytes.Buffer</a>,
<a href="https://golang.org/pkg/strings/#Builder">strings.Builder</a>
and others to construct strings. <br />
If you need advanced unicode support, check out the <a
href="https://golang.org/pkg/unicode/">unicode</a> package.
`,
'ext': `
Go files ends with <code>.go</code> extension. The source code should be
<a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a> encoded.
<br /><br />
Several <code>.go</code> files can be in the same directory and construct a
package.
`,
'semicolon': `
Go statements don't need to end with semicolon (<code>;</code>). You <i>can</i>
add them and the program will compile, but idiomatic Go code don't have
semicolons in it.
`,
};
var initial_help = `
This is an annoated
<a href="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program" target="_blank">"Hello World"</a>
<a target="_blank" href="https://golang.org/">Go</a> program. Hover around the
code, then click on highlighted words. You can use the checkbox on the top
right to highlight all.
`;
</script>
</html>