-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-basics.html
executable file
·392 lines (314 loc) · 23.8 KB
/
go-basics.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/theme/css/elegant.prod.9e9d5ce754.css" media="screen">
<link rel="stylesheet" type="text/css" href="/theme/css/custom.css" media="screen">
<link rel="dns-prefetch" href="//fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
<meta name="author" content="Parijatha Kumar" />
<meta property="og:type" content="article" />
<meta name="twitter:card" content="summary">
<meta name="keywords" content="go, coding, Go, " />
<meta property="og:title" content="Go -- Basics "/>
<meta property="og:url" content="/go-basics.html" />
<meta property="og:description" content="Basic structure, types and variable declaration" />
<meta property="og:site_name" content="while True: print("Keep learning")" />
<meta property="og:article:author" content="Parijatha Kumar" />
<meta property="og:article:published_time" content="2020-04-22T17:30:00+05:30" />
<meta name="twitter:title" content="Go -- Basics ">
<meta name="twitter:description" content="Basic structure, types and variable declaration">
<title>Go -- Basics · while True: print("Keep learning")
</title>
</head>
<body>
<div id="content">
<div class="navbar navbar-static-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="/"><span class=site-name>while True: print("Keep learning")</span></a>
<div class="nav-collapse collapse">
<ul class="nav pull-right top-menu">
<li >
<a href=
"/"
>Home</a>
</li>
<li ><a href="/categories.html">Categories</a></li>
<li ><a href="/tags.html">Tags</a></li>
<li ><a href="/archives.html">Archives</a></li>
<li><form class="navbar-search" action="/search.html" onsubmit="return validateForm(this.elements['q'].value);"> <input type="text" class="search-query" placeholder="Search" name="q" id="tipue_search_input"></form></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span1"></div>
<div class="span10">
<article itemscope>
<div class="row-fluid">
<header class="page-header span10 offset2">
<h1>
<a href="/go-basics.html">
Go -- Basics
</a>
</h1>
</header>
</div>
<div class="row-fluid">
<div class="span8 offset2 article-content">
<h2>Program Structure and Environment</h2>
<p>Source files have ".go" extension.</p>
<p>Every file starts with a package namespace declaration. All ".go" files in the same directory must have the same package declaration and thus belong to the same package.</p>
<p>Program execution starts at the <code>main()</code> function in the <code>main</code> package. So, for the program to be executable ...</p>
<ul>
<li>there must be a <code>main</code> package. This is somewhat similar to Java where there must be a <code>Main.java</code> file. In Java, the file itself must be named as <code>Main.java</code>, whereas in Go, only the package namespace must be called <code>main</code>, the directory name can be anything.</li>
<li>there must be one and only one file in that <code>main</code> package with a <code>main()</code> function. This is also similar to Java where there must be a <code>Main</code> class (in <code>Main.java</code> file) having a function with a specific signature (<code>public static void main(String[] args)</code>).</li>
</ul>
<p>There is no such requirement for Python though we often use the <code>if __name__ == "__main__":</code> pattern.</p>
<p>If the program is not required to be executed on it's own, then there need not be a <code>main</code> package.</p>
<div class="highlight"><pre><span></span><span class="c1">// 'package' statement namespaces this file.</span>
<span class="c1">// main package is compulsory for executable programs.</span>
<span class="kn">package</span> <span class="nx">main</span>
<span class="kn">import</span> <span class="s">"fmt"</span>
<span class="c1">// main() function is the entry point of the program (not just this single file).</span>
<span class="c1">// 'main' package MUST have a function named main().</span>
<span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"hello world"</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
<p>Compile with <code>go build file_name.go</code>. Or else build and run with <code>go run file_name.go</code>.</p>
<p>Go needs semi-colons as the statement terminators as per grammar rules. But the lexer will auto-insert one if there is no ambiguity. Because of this, Go recommends that we avoid putting semi-colon on each line unless it is absolutely necessary. In fact, Go's own auto-formatting tool <code>gofmt</code> will remove unnecessary semi-colons. This <a href="https://groups.google.com/forum/#!topic/golang-nuts/XuMrWI0Q8uk">post</a> is interesting to read.</p>
<p>Because of this auto-inserting-semi-colon feature, there is a tricky side-effect to understand. The <code>{</code> on the same line at the end of the function definition above (<code>func main() {</code>) is not a style, it is a must. If it is omitted (for example, if we want to style it by entering the opening curly brace on the next line), Go will insert a semi-colon after <code>main()</code> and thus the next line's <code>{</code> doesn't have any meaning by itself and thus the program doesn't compile.</p>
<p>A function or variable can be imported from other packages only if it's name starts with a capital letter. Strange. Also, unlike Python, Go programs don't compile if we import a function and don't use it. But there is a workaround for it by placing an underscore in front of the imported function name.</p>
<p>Comments are more like Java. <code>//</code> and <code>/* ...... */</code>.</p>
<h2>Basics</h2>
<h3>Variables and Constants</h3>
<p>Variables can be declared in a few different ways ...</p>
<div class="highlight"><pre><span></span><span class="kn">package</span> <span class="nx">main</span>
<span class="c1">// package scope</span>
<span class="c1">// When declaring in package scope, the keyword var is compulsory because</span>
<span class="c1">// any statement in package scope must start with a Go keyword.</span>
<span class="c1">// So months := 35 is invalid here.</span>
<span class="kd">var</span> <span class="nx">months</span> <span class="kt">int</span> <span class="p">=</span> <span class="mi">35</span>
<span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
<span class="c1">// function scope of function 'main'</span>
<span class="c1">// the := shortcut works only in the function scope.</span>
<span class="nx">distanceInKm</span> <span class="o">:=</span> <span class="mf">43.5</span> <span class="c1">// infers the type as float</span>
<span class="c1">// multiple variables can also be initialised at once with := shortcut</span>
<span class="nx">name</span><span class="p">,</span> <span class="nx">knowEnglish</span><span class="p">,</span> <span class="nx">age</span> <span class="o">:=</span> <span class="s">"Lakshmi"</span><span class="p">,</span> <span class="kc">true</span><span class="p">,</span> <span class="mi">38</span>
<span class="c1">// we can use var in function scope too</span>
<span class="kd">var</span> <span class="nx">years</span> <span class="kt">int</span> <span class="p">=</span> <span class="mi">35</span>
<span class="c1">// multiple variables can be declared in one line with var</span>
<span class="kd">var</span> <span class="nx">apples</span><span class="p">,</span> <span class="nx">oranges</span> <span class="kt">int</span> <span class="c1">// d and e get zero value of int type i.e., 0</span>
<span class="kd">var</span> <span class="nx">firstName</span><span class="p">,</span> <span class="nx">secondName</span> <span class="kt">string</span> <span class="c1">// f and g get zero value of string type i.e., ""</span>
<span class="c1">// multiple variables can be declared and optionally</span>
<span class="c1">// initialised at once like so ...</span>
<span class="kd">var</span> <span class="p">(</span>
<span class="nx">person</span> <span class="kt">string</span> <span class="p">=</span> <span class="s">"Nani"</span>
<span class="nx">isMarried</span> <span class="kt">bool</span> <span class="p">=</span> <span class="kc">true</span>
<span class="nx">isEmployed</span> <span class="kt">bool</span>
<span class="p">)</span>
<span class="p">}</span>
</pre></div>
<p>As Go is UTF-8 compliant, variable names need not be in English only. They can be in any language. Only rule is it must start with either a letter or <code>_</code> and then it can contain letters, digits and <code>_</code>.</p>
<p>The zero values are :</p>
<ul>
<li>0 for a number type like <code>int</code> and <code>float</code></li>
<li>"" for <code>string</code> type</li>
<li><code>false</code> for <code>bool</code> type</li>
<li>The special <code>nil</code> for other types (pointers, functions, interfaces, slices, channels and maps)</li>
</ul>
<p>Constants can be declared just like variables, with a <code>const</code> keyword instead.</p>
<div class="highlight"><pre><span></span><span class="kd">const</span> <span class="nx">a</span> <span class="p">=</span> <span class="mi">32</span> <span class="c1">// := operator not allowed for const declarations</span>
<span class="kd">const</span> <span class="nx">b</span> <span class="kt">int</span> <span class="p">=</span> <span class="mi">64</span>
<span class="kd">const</span> <span class="p">(</span>
<span class="nx">c</span> <span class="p">=</span> <span class="mf">32.5</span>
<span class="nx">d</span> <span class="kt">string</span> <span class="p">=</span> <span class="s">"Venu"</span>
<span class="p">)</span>
</pre></div>
<p>When we want to create some sort of Enum functioality, we can use the <code>iota</code> keyword and <code>const</code> declarations to create one.</p>
<div class="highlight"><pre><span></span><span class="kd">const</span> <span class="p">(</span>
<span class="nx">Sunday</span> <span class="p">=</span> <span class="kc">iota</span>
<span class="nx">Monday</span>
<span class="nx">Tuesday</span>
<span class="nx">Wednesday</span>
<span class="nx">Thursday</span>
<span class="nx">Friday</span>
<span class="nx">Saturday</span>
<span class="p">)</span>
<span class="c1">// iota auto assigns the numbers.</span>
<span class="c1">// iota also takes care when we later add another constant in between.</span>
</pre></div>
<p>In Go, a scope can be created with <code>{</code> <code>}</code> brackets just like in Java and JavaScript. In Python, indented block creates a scope.</p>
<h3>Core Types</h3>
<ul>
<li>booleans : <code>bool</code>. Can be <code>true</code> and <code>false</code>. In Python, they are <code>True</code> and <code>False</code>.</li>
<li>integers : <code>uint8</code>, <code>uint16</code>,<code>uint32</code>, <code>uint64</code> and <code>int8</code>, <code>int16</code>, <code>int32</code> and <code>int64</code>. <code>byte</code> is alias for <code>uint8</code>. <code>rune</code> is alias for <code>int32</code>. Also, there are <code>uint</code> and <code>int</code> which are platform dependent (32-bit or 64-bit). Important thing to note is that <code>int</code> is a different type from either of the <code>int32</code> or <code>int64</code> types, even if the values are equal. If u need even bigger numbers than <code>uint32</code> or <code>uint64</code>, then u can use <code>math/big</code>. Also, one more thing to note is if we try to assign values above the maximum allowed for that type, then Go raises an "overflows" error. But, if we increment the number beyond the max allowed one in the code, then Go will not raise any error, instead, it silently wraps around from the smallest number side of that type. This is dangerous if not taken care.</li>
<li>floats : <code>float32</code> and <code>float64</code>.</li>
<li>text : </li>
<li><code>string</code> : Go strings are simple slices (arrays) of bytes. Nothing more than that. As Go needs to support more than one type of encoding (though the default preferred one is UTF-8), Go strings do not assume any encoding on their own. So, they are just sequence of raw bytes. This is similar to the setup in Python2. That's why <code>len()</code> on a string variable gives only the number of raw bytes, not the number of characters.</li>
<li><code>rune</code> : It's character type that stores a single UTF-8 multi-byte character. To figure out the number of characters of a string variable, we need to make a slice of <code>rune</code> type from it, and then apply <code>len()</code> on that slice. Or else, we can use <code>range</code> on the string variable, which iterates over the characters (of type <code>rune</code>) one by one.</li>
<li>the special <code>nil</code>: In fact, it's not a type. It's a special value which represents empty value of empty type.</li>
</ul>
<h3>Literals</h3>
<p>Literals are the way you provide values to a variable.</p>
<p>Go supports two types of string literals.</p>
<ul>
<li>raw-strings : enclosed in back-quotes (`). Even "\n" is also not interpreted as new line.</li>
<li>interpreted strings : enclosed in double quotes <code>"</code>. </li>
</ul>
<p>For <code>rune</code> type, the character is to be surrounded with single quotes. Ex. <code>var a rune = 'H'</code>. Any single character surrounded by single quotes is interpreted as <code>rune</code> type by Go.</p>
<h3>Operators</h3>
<p>Go does not have a separate operator for integer division unlike Python (<code>//</code>). The division operator, <code>/</code>, in Go acts much like that in Java and other statically typed languages. See ...</p>
<div class="highlight"><pre><span></span><span class="mi">15</span> <span class="o">/</span> <span class="mi">2</span> <span class="c1">// acts like integer division, results in 7</span>
<span class="mf">15.0</span> <span class="o">/</span> <span class="mi">2</span> <span class="c1">// results in 7.5</span>
<span class="mi">15</span> <span class="o">/</span> <span class="mf">2.0</span> <span class="c1">// results in 7.5</span>
<span class="mf">15.0</span> <span class="o">/</span> <span class="mf">2.0</span> <span class="c1">// results in 7.5</span>
</pre></div>
<p>Logical Operators : <code>&&</code>, <code>||</code> and <code>!</code>. These are just like those in Java and JavaScript. The equivalent ones in Python are <code>and</code>, <code>or</code>, and <code>not</code>.</p>
<p>Go has the familiar bit-wise operators (<code>&</code>, <code>|</code>, <code>^</code>, <code><<</code> and <code>>></code> ) as well.</p>
<h3>Basic Printing</h3>
<p><code>fmt.Println()</code>, <code>fmt.Print()</code>, and <code>fmt.Printf()</code> are the simplest and widely used printing functions. </p>
<p><code>fmt.Print()</code> adds a space between parameters and also a new line at the end.</p>
<p><code>fmt.Printf()</code> takes format strings. It doesn't add any spaces between paramaters, or a newline at the end. A few common formatting patterns are ...</p>
<ul>
<li><code>%d</code> : decimal (base 10) number</li>
<li><code>%s</code> : string</li>
<li><code>%T</code> : type of the variable</li>
<li><code>%v</code> : any value (use when u don't know exact type)</li>
<li><code>%+v</code> : any value, but with extra information for special types (ex. <code>struct</code> fields)</li>
<li><code>%#v</code>: same as <code>%+v</code> but also prints the type of the variables for certain types.</li>
</ul>
<p>Besides, there is also the <code>S</code> family of functions that includes <code>fmt.Sprintln()</code>, <code>fmt.Sprint()</code>, and <code>fmt.Sprintf()</code>, which are used for creating strings based on the given format and the <code>F</code> family of functions that includes <code>fmt.Fprintln()</code>, <code>fmt.Fprint()</code> and <code>fmt.Fprintf()</code>, which are used for writing to files using an <code>io.Writer</code>.</p>
<h3>Pointers</h3>
<p>Like other languages .... <code>*</code> operator dereferences a pointer (gives the value stored at the memory address pointed to by the pointer), <code>&</code> operator gives the memory address of a variable.</p>
<p>The zero value of a pointer type is <code>nil</code>. As Go allows <code>nil</code> pointers, i.e., pointers to values that do not exist, it is better to check for <code>pointer != nil</code> condition before trying to dereference a pointer, otherwise, it will result in run-time errors.</p>
<p>Pointers can be created in a few ways ...</p>
<ul>
<li><code>var a *int</code> : Here, <code>a</code> is declared as a pointer type (to integer values). But it's value is <code>nil</code>. No memory address is allocated yet. If you Printf it with <code>#v</code>, then you get something like <code>(*int)(nil)</code>.</li>
<li><code>var a = new(int)</code>or <code>a := new(int)</code>: A memory location is allotted to store <code>int</code> values. The memory location is initialised with the default zero value of 0. If you Printf it with <code>#v</code>, then you get some memory location like <code>(*int)(0xc00002c008)</code>.</li>
<li><code>a := &b</code>: Here, <code>b</code> must already be declared (anyhow it must have been auto-initialised with the zero value). Some special structs allow creating pointers to them without even declaring them first. Ex. <code>a := &time.Time{}</code>.</li>
</ul>
<p>Function arguments which are normal values (copies) are managed by Go (most of the times) in stacks. Whereas, function arguments which are pointers (pass by reference) are managed by Go in heap memory.</p>
<p>Memory in heaps is reclaimed by a complex Garbage collection process and thus managing memory in heaps is comparatively more CPU-intensive than that in stacks.</p>
<hr/>
</div>
<section id="article-sidebar" class="span2">
<h4>Published</h4>
<time itemprop="dateCreated" datetime="2020-04-22T17:30:00+05:30">Wed 22 April 2020</time>
<h4>Category</h4>
<a class="category-link" href="/categories.html#go-ref">Go</a>
<h4>Tags</h4>
<ul class="list-of-tags tags-in-article">
<li><a href="/tags.html#coding-ref">coding
<span class="superscript">5</span>
</a></li>
<li><a href="/tags.html#go-ref">go
<span class="superscript">4</span>
</a></li>
</ul>
</section>
</div>
</article>
<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
<!-- Background of PhotoSwipe.
It's a separate element as animating opacity is faster than rgba(). -->
<div class="pswp__bg"></div>
<!-- Slides wrapper with overflow:hidden. -->
<div class="pswp__scroll-wrap">
<!-- Container that holds slides.
PhotoSwipe keeps only 3 of them in the DOM to save memory.
Don't modify these 3 pswp__item elements, data is added later on. -->
<div class="pswp__container">
<div class="pswp__item"></div>
<div class="pswp__item"></div>
<div class="pswp__item"></div>
</div>
<!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
<div class="pswp__ui pswp__ui--hidden">
<div class="pswp__top-bar">
<!-- Controls are self-explanatory. Order can be changed. -->
<div class="pswp__counter"></div>
<button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
<button class="pswp__button pswp__button--share" title="Share"></button>
<button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
<button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
<!-- Preloader demo https://codepen.io/dimsemenov/pen/yyBWoR -->
<!-- element will get class pswp__preloader--active when preloader is running -->
<div class="pswp__preloader">
<div class="pswp__preloader__icn">
<div class="pswp__preloader__cut">
<div class="pswp__preloader__donut"></div>
</div>
</div>
</div>
</div>
<div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
<div class="pswp__share-tooltip"></div>
</div>
<button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
</button>
<button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
</button>
<div class="pswp__caption">
<div class="pswp__caption__center"></div>
</div>
</div>
</div>
</div> </div>
<div class="span1"></div>
</div>
</div>
</div>
<footer>
<div id="fpowered">
Powered by: <a href="http://getpelican.com/" title="Pelican Home Page" target="_blank" rel="nofollow noopener noreferrer">Pelican</a>
Theme: <a href="https://elegant.oncrashreboot.com/" title="Theme Elegant Home Page" target="_blank" rel="nofollow noopener noreferrer">Elegant</a>
</div>
</footer> <script src="//code.jquery.com/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
<script src="/theme/js/elegant.prod.9e9d5ce754.js"></script>
<script>
function validateForm(query)
{
return (query.length > 0);
}
</script>
<script>
(function () {
if (window.location.hash.match(/^#comment-\d+$/)) {
$('#comment_thread').collapse('show');
}
})();
window.onhashchange=function(){
if (window.location.hash.match(/^#comment-\d+$/))
window.location.reload(true);
}
$('#comment_thread').on('shown', function () {
var link = document.getElementById('comment-accordion-toggle');
var old_innerHTML = link.innerHTML;
$(link).fadeOut(200, function() {
$(this).text('Click here to hide comments').fadeIn(200);
});
$('#comment_thread').on('hidden', function () {
$(link).fadeOut(200, function() {
$(this).text(old_innerHTML).fadeIn(200);
});
})
})
</script>
</body>
<!-- Theme: Elegant built for Pelican
License : MIT -->
</html>