tag:blogger.com,1999:blog-33241737397697501062024-03-27T23:53:21.143+00:00My journey into Go (GoLang)My progress in learning the programming language Go from Google in the form of a daily blogAnonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.comBlogger20125tag:blogger.com,1999:blog-3324173739769750106.post-39761667844153228582015-09-29T06:04:00.001+00:002015-11-08T22:32:58.078+00:00Day 20 - A go http/2 server exampleI was really excited when i read that <a href="https://www.reddit.com/r/golang/comments/3m6pfm/package_http2_now_on_golangorgxnethttp2/" target="_blank">support for http2 is now on golang.org</a>! I was thinking about checking out <a href="https://en.wikipedia.org/wiki/SPDY">SPDY</a> in go for a long time, but now <a href="https://en.wikipedia.org/wiki/HTTP/2">HTTP/2</a> support moved one important step further.<br />
<br />
I think its one of the "Next big Things" on the interweb (the last change to http was 18! years ago)<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEAK8_4Vc4Cfw45d7V4wAuxRj61EagJ3ehkg3NL0clMaqLKjHcIwidpHmUOTYov5eaE_fF0hVy1hfRXTas2S2mjr6bc7jf0e74Zq_kMTDfX0XIjc4MjhRpYV1SQx4y47be3PPcKLnv29c/s1600/http2_next_big_thing_small.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEAK8_4Vc4Cfw45d7V4wAuxRj61EagJ3ehkg3NL0clMaqLKjHcIwidpHmUOTYov5eaE_fF0hVy1hfRXTas2S2mjr6bc7jf0e74Zq_kMTDfX0XIjc4MjhRpYV1SQx4y47be3PPcKLnv29c/s1600/http2_next_big_thing_small.jpg" /></a></div>
<br />
So i skip SPDY and go directly for HTTP/2 - this post is about how to get a first grip and test it serverside as a go html server example.<br />
<br />
Test preparations clientside:<br />
<ol>
<li>Get a browser that has http/2 support enabled by default: <a href="https://www.google.com/chrome/browser/canary.html" target="_blank">Chrome Canary</a></li>
<li>Download the <a href="https://chrome.google.com/webstore/detail/http2-and-spdy-indicator/mpbpobfflnpcgagjijhmgnchggcjblin" target="_blank">http2 and spdy indicator addon for chrome</a> </li>
<li>Test your browsers http2 support at: <a href="https://http2.akamai.com/demo" target="_blank">https://http2.akamai.com/demo</a></li>
</ol>
Serverside::<br />
<ol>
<li>Cd into your <span style="font-family: "courier new" , "courier" , monospace;">GOPATH/src</span></li>
<li>Run <span style="font-family: "courier new" , "courier" , monospace;">go get golang.org/x/net/http2</span> </li>
<li>Check if the package has been downloaded</li>
</ol>
Create, build and run:<br />
<ol>
<li>Create a new go project, name it eg. testHttp2Server</li>
<li>Create a ssl certificate (localhost.cert and localhost.key)<br />You can ether generate a self signed cert: <a href="http://www.selfsignedcertificate.com/" target="_blank">selfsignedcertificate</a><br />or use mine: <a href="https://github.com/kimxilxyong/intogooglego/tree/master/testHttp2Server" target="_blank">testHttp2Server</a><br />This two files must be in the same directory as the server binary.</li>
<li>Code for http2Server.go:<br /><br /><br /><span style="color: navy; font-weight: 600;">package</span><span style="color: silver;"> </span>main
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">import</span><span style="color: silver;"> </span><span style="color: black;">(</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"fmt"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"golang.org/x/net/http2"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"html"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"log"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"net/http"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">main()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">var</span><span style="color: silver;"> </span>srv<span style="color: silver;"> </span>http<span style="color: black;">.</span>Server</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>http2<span style="color: black;">.</span>VerboseLogs<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span><span style="color: navy;">true</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>srv<span style="color: black;">.</span>Addr<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span><span style="color: green;">":8080"</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">This</span><span style="color: silver;"> </span><span style="color: green;">enables</span><span style="color: silver;"> </span><span style="color: green;">http2</span><span style="color: silver;"> </span><span style="color: green;">support</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>http2<span style="color: black;">.</span>ConfigureServer<span style="color: black;">(&</span>srv<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Plain</span><span style="color: silver;"> </span><span style="color: green;">text</span><span style="color: silver;"> </span><span style="color: green;">test</span><span style="color: silver;"> </span><span style="color: green;">handler</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Open</span><span style="color: silver;"> </span><span style="color: green;">https://localhost:8080/randomtest</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">in</span><span style="color: silver;"> </span><span style="color: green;">your</span><span style="color: silver;"> </span><span style="color: green;">Chrome</span><span style="color: silver;"> </span><span style="color: green;">Canary</span><span style="color: silver;"> </span><span style="color: green;">browser</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>http<span style="color: black;">.</span>HandleFunc<span style="color: black;">(</span><span style="color: green;">"/"</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">func</span><span style="color: black;">(</span>w<span style="color: silver;"> </span>http<span style="color: black;">.</span>ResponseWriter<span style="color: black;">,</span><span style="color: silver;"> </span>r<span style="color: silver;"> </span><span style="color: black;">*</span>http<span style="color: black;">.</span>Request<span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Fprintf<span style="color: black;">(</span>w<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"Hi</span><span style="color: silver;"> </span><span style="color: green;">tester</span><span style="color: silver;"> </span><span style="color: green;">%q\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>html<span style="color: black;">.</span>EscapeString<span style="color: black;">(</span>r<span style="color: black;">.</span>URL<span style="color: black;">.</span>Path<span style="color: black;">))</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>ShowRequestInfoHandler<span style="color: black;">(</span>w<span style="color: black;">,</span><span style="color: silver;"> </span>r<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">})</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Listen</span><span style="color: silver;"> </span><span style="color: green;">as</span><span style="color: silver;"> </span><span style="color: green;">https</span><span style="color: silver;"> </span><span style="color: green;">ssl</span><span style="color: silver;"> </span><span style="color: green;">server</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">NOTE:</span><span style="color: silver;"> </span><span style="color: green;">WITHOUT</span><span style="color: silver;"> </span><span style="color: green;">SSL</span><span style="color: silver;"> </span><span style="color: green;">IT</span><span style="color: silver;"> </span><span style="color: green;">WONT</span><span style="color: silver;"> </span><span style="color: green;">WORK!!</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">To</span><span style="color: silver;"> </span><span style="color: green;">self</span><span style="color: silver;"> </span><span style="color: green;">generate</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">test</span><span style="color: silver;"> </span><span style="color: green;">ssl</span><span style="color: silver;"> </span><span style="color: green;">cert/key</span><span style="color: silver;"> </span><span style="color: green;">you</span><span style="color: silver;"> </span><span style="color: green;">could</span><span style="color: silver;"> </span><span style="color: green;">go</span><span style="color: silver;"> </span><span style="color: green;">to</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">http://www.selfsignedcertificate.com/</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">or</span><span style="color: silver;"> </span><span style="color: green;">read</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">openssl</span><span style="color: silver;"> </span><span style="color: green;">manual</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>log<span style="color: black;">.</span>Fatal<span style="color: black;">(</span>srv<span style="color: black;">.</span>ListenAndServeTLS<span style="color: black;">(</span><span style="color: green;">"localhost.cert"</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"localhost.key"</span><span style="color: black;">))</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">ShowRequestInfoHandler(</span>w<span style="color: silver;"> </span>http<span style="color: black;">.</span>ResponseWriter<span style="color: black;">,</span><span style="color: silver;"> </span>r<span style="color: silver;"> </span><span style="color: black;">*</span>http<span style="color: black;">.</span>Request<span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>w<span style="color: black;">.</span>Header<span style="color: black;">().</span>Set<span style="color: black;">(</span><span style="color: green;">"Content-Type"</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"text/plain"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Fprintf<span style="color: black;">(</span>w<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"Method:</span><span style="color: silver;"> </span><span style="color: green;">%s\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>r<span style="color: black;">.</span>Method<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Fprintf<span style="color: black;">(</span>w<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"Protocol:</span><span style="color: silver;"> </span><span style="color: green;">%s\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>r<span style="color: black;">.</span>Proto<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Fprintf<span style="color: black;">(</span>w<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"Host:</span><span style="color: silver;"> </span><span style="color: green;">%s\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>r<span style="color: black;">.</span>Host<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Fprintf<span style="color: black;">(</span>w<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"RemoteAddr:</span><span style="color: silver;"> </span><span style="color: green;">%s\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>r<span style="color: black;">.</span>RemoteAddr<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Fprintf<span style="color: black;">(</span>w<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"RequestURI:</span><span style="color: silver;"> </span><span style="color: green;">%q\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>r<span style="color: black;">.</span>RequestURI<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Fprintf<span style="color: black;">(</span>w<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"URL:</span><span style="color: silver;"> </span><span style="color: green;">%#v\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>r<span style="color: black;">.</span>URL<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Fprintf<span style="color: black;">(</span>w<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"Body.ContentLength:</span><span style="color: silver;"> </span><span style="color: green;">%d</span><span style="color: silver;"> </span><span style="color: green;">(-1</span><span style="color: silver;"> </span><span style="color: green;">means</span><span style="color: silver;"> </span><span style="color: green;">unknown)\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>r<span style="color: black;">.</span>ContentLength<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Fprintf<span style="color: black;">(</span>w<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"Close:</span><span style="color: silver;"> </span><span style="color: green;">%v</span><span style="color: silver;"> </span><span style="color: green;">(relevant</span><span style="color: silver;"> </span><span style="color: green;">for</span><span style="color: silver;"> </span><span style="color: green;">HTTP/1</span><span style="color: silver;"> </span><span style="color: green;">only)\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>r<span style="color: black;">.</span>Close<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Fprintf<span style="color: black;">(</span>w<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"TLS:</span><span style="color: silver;"> </span><span style="color: green;">%#v\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>r<span style="color: black;">.</span>TLS<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Fprintf<span style="color: black;">(</span>w<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"\nHeaders:\n"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>r<span style="color: black;">.</span>Header<span style="color: black;">.</span>Write<span style="color: black;">(</span>w<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
</li>
<li>Compile: <span style="font-family: "courier new" , "courier" , monospace;">go build http2Server.go</span></li>
<li>Run the http2Server binary</li>
</ol>
<br />
Test it:<br />
<ol>
<li>Open in Chrome Canary:<span style="font-family: "courier new" , "courier" , monospace;"> https://localhost:8080/kim</span></li>
<li>Accept the certificate security error, continue to localhost </li>
<li>Check if Protocol is HTTP/2.0, example output should be like:<br /><br /><pre style="-webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 1; word-spacing: 0px; word-wrap: break-word;">Hello tester "/kim"
Method: GET
<span style="background-color: #cccccc;"><span style="color: #274e13;">Protocol: HTTP/2.0</span></span>
Host: localhost:8080</pre>
<pre style="-webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 1; word-spacing: 0px; word-wrap: break-word;">... </pre>
</li>
</ol>
<br />
NOTE: http/2 in go ONLY WORKS WITH SSL/TLS! (currently)<br />
I not sure why, but i did not got the server to support http/2 without TLS!<br />
This could be a clientside or serverside issue - i dont know currently. <br />
<br />
Some discussions about this:<br />
<a href="http://security.stackexchange.com/questions/72295/why-wouldnt-it-be-great-if-http-2-would-only-allow-communication-via-tls" target="_blank">why-wouldnt-it-be-great-if-http-2-would-only-allow-communication-via-tls</a><br />
<a href="http://daniel.haxx.se/blog/2015/03/06/tls-in-http2/" target="_blank">daniel.haxx.se tls-in-http2</a><br />
<a href="http://arstechnica.com/information-technology/2015/02/http2-finished-coming-to-browsers-within-weeks/" target="_blank">arstechnica http2-finished-coming-to-browsers-within-weeks</a><br />
<br />
As far as i have understood it, the spec says it "should be" possible to use it without the need to buy SSL certificates:<br />
<a href="https://http2.github.io/http2-spec/" target="_blank">https://http2.github.io/http2-spec/</a><br />
<a href="https://tools.ietf.org/html/rfc7540" target="_blank">rfc7540 - html2</a><br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com11tag:blogger.com,1999:blog-3324173739769750106.post-59498931898482300722015-09-19T04:09:00.001+00:002015-09-19T04:12:03.836+00:00Day 19 - Upgrading to Go 1.5.1 on Win7 64I upgraded to Go 1.5.1 from 1.4.2 today<br />
<br />
0:) Deinstall old version of Go (<a href="https://golang.org/doc/install" target="_blank">doc/install</a>) <br />
<br />
1:) Download <a href="https://storage.googleapis.com/golang/go1.5.1.windows-amd64.msi">https://storage.googleapis.com/golang/go1.5.1.windows-amd64.msi</a><br />
<br />
2:) Get worried by this message from chrome:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2Aod3imkNpa3BUusjs6wiNMKgorklEq0xIvOrHpW85u5Y6DMRqLfkwEKBruGbvMYpvsyoHxgbgdIsscXrhiWFtHfo1SWdCBlOlExXRezr4-fMwLPUIjYe1B2hygscm5-D-ym7USKPALE/s1600/Go151_msi_warning.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2Aod3imkNpa3BUusjs6wiNMKgorklEq0xIvOrHpW85u5Y6DMRqLfkwEKBruGbvMYpvsyoHxgbgdIsscXrhiWFtHfo1SWdCBlOlExXRezr4-fMwLPUIjYe1B2hygscm5-D-ym7USKPALE/s1600/Go151_msi_warning.png" /></a></div>
<br />
It says that there is something fishy with the download and could be harmful.<br />
<br />
3:) Go on and accept it - its from google, so what ...<br />
<br />
4:) Go to the download folder and run your virus scanner over the msi:<br />
<br />
5:) UhOh - WTF is an Archive Bomb?<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg524a968GBfzpcDrihyphenhyphenimVHgPqqtLIX2bjI6wFe4ZACJwWwGVbt7ll2JtoN01ff65LmJBw-snnhR5e1MUeJywR0SpEkZVBWTVJBLax-14xX-AP8xA0tBCJYXfW2SEW6hXe7iXDvR-Dd5A/s1600/Go151_msi_archivebomb.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="60" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg524a968GBfzpcDrihyphenhyphenimVHgPqqtLIX2bjI6wFe4ZACJwWwGVbt7ll2JtoN01ff65LmJBw-snnhR5e1MUeJywR0SpEkZVBWTVJBLax-14xX-AP8xA0tBCJYXfW2SEW6hXe7iXDvR-Dd5A/s640/Go151_msi_archivebomb.png" width="640" /></a></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
6:) Google for Archive Bomb, read Wiki about <a href="https://en.wikipedia.org/wiki/Zip_bomb" target="_blank">Zip_bomb</a><br />
<br />
7:) Looks like its detected because of special compression used by bzip2, i feel brave today and go on installing the msi.<br />
<br />
8.) Checking again the c:\go folder for viruses - all ok<br />
<br />
9:)<br />
<span style="font-family: "Courier New",Courier,monospace;">D:\>go version<br />go version go1.5.1 windows/amd64 </span><br />
<br />
Yeah, it works<br />
<br />
10:) Recompiling and running some of my most important projects - no problems whatsoever.<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com1tag:blogger.com,1999:blog-3324173739769750106.post-85579770520924140092015-08-29T14:18:00.002+00:002015-09-07T03:04:56.295+00:00Day 18 - How to get a token from a JWT serverThere is a wonderful middleware for <a href="https://github.com/ant0ine/go-json-rest" target="_blank">ant0ine/go-json-rest</a> to implement JWT for go: <a href="https://github.com/StephanDollberg/go-json-rest-middleware-jwt" target="_blank">go-json-rest-middleware-jwt</a><br />
<br />
The problem I had is that the docu seems not to be working for curl on windows, the go code is alright, only the curl command line is a problem:<br />
<br />
So here is a windows example of how to make a test call with curl to get a JWT token:<br />
<br />
-- snip<br />
<span style="font-family: "Courier New",Courier,monospace;">curl -X POST --data "{\"username\": \"admin\", \"password\": \"admin\"}" -H "Content-Type:application/json" http://localhost:8080/login</span><br />
-- snip<br />
<br />
-- output should be something like this:<br />
<span style="font-family: "Courier New",Courier,monospace;">"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0NDA4NjAzMzgsImlkIjoiYWRtaW4iLCJvcmlnX2lhdCI6MTQ0MDg1NjczOH0.cIIACBWOFCMZI4vBgSdQXkUsm8f7WpRoSoFtmoOtceQ"</span><br />
--output <br />
<br />
Docu of jwt with json-rest<br />
<a href="https://github.com/ant0ine/go-json-rest#jwt" target="_blank">https://github.com/ant0ine/go-json-rest#jwt</a><br />
<br />
Full source of my example app:<br />
<a href="https://github.com/kimxilxyong/intogooglego/tree/master/restPostServer" target="_blank">intogooglego/restPostServer</a><br />
<br />
<br />
<span style="color: green;"></span>--- demo ---<br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>i<span style="color: black;">.</span>jwt_middleware<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span><span style="color: black;">&</span>jwt<span style="color: black;">.</span>JWTMiddleware<span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Key<span style="color: black;">:</span><span style="color: silver;"> </span><span style="color: black;">[]</span><span style="color: navy;">byte</span><span style="color: black;">(</span><span style="color: green;">"secret</span><span style="color: silver;"> </span><span style="color: green;">key"</span><span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Realm<span style="color: black;">:</span><span style="color: silver;"> </span><span style="color: green;">"jwt</span><span style="color: silver;"> </span><span style="color: green;">auth"</span><span style="color: black;">,</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Timeout<span style="color: black;">:</span><span style="color: silver;"> </span>time<span style="color: black;">.</span>Hour<span style="color: black;">,</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>MaxRefresh<span style="color: black;">:</span><span style="color: silver;"> </span>time<span style="color: black;">.</span>Hour<span style="color: silver;"> </span><span style="color: black;">*</span><span style="color: silver;"> </span><span style="color: purple;">24</span><span style="color: black;">,</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Authenticator<span style="color: black;">:</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">func</span><span style="color: black;">(</span>userId<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: black;">,</span><span style="color: silver;"> </span>password<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: navy;">bool</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>debugLevel<span style="color: silver;"> </span><span style="color: black;">></span><span style="color: silver;"> </span><span style="color: purple;">2</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Printf<span style="color: black;">(</span><span style="color: green;">"Authenticator:</span><span style="color: silver;"> </span><span style="color: green;">'%s'</span><span style="color: silver;"> </span><span style="color: green;">-</span><span style="color: silver;"> </span><span style="color: green;">'%s'\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>userId<span style="color: black;">,</span><span style="color: silver;"> </span>password<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>userId<span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span><span style="color: green;">"admin"</span><span style="color: silver;"> </span><span style="color: black;">&&</span><span style="color: silver;"> </span>password<span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span><span style="color: green;">"admin"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">},</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>api<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>rest<span style="color: black;">.</span>NewApi<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">var</span><span style="color: silver;"> </span>MiddleWareStack<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span><span style="color: black;">[]</span>rest<span style="color: black;">.</span>Middleware<span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">&</span>rest<span style="color: black;">.</span>AccessLogApacheMiddleware<span style="color: black;">{},</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">&</span>rest<span style="color: black;">.</span>TimerMiddleware<span style="color: black;">{},</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">&</span>rest<span style="color: black;">.</span>RecorderMiddleware<span style="color: black;">{},</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//&rest.PoweredByMiddleware{},</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">&</span>rest<span style="color: black;">.</span>RecoverMiddleware<span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>EnableResponseStackTrace<span style="color: black;">:</span><span style="color: silver;"> </span><span style="color: navy;">true</span><span style="color: black;">,</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">},</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">&</span>rest<span style="color: black;">.</span>JsonIndentMiddleware<span style="color: black;">{},</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">&</span>rest<span style="color: black;">.</span>ContentTypeCheckerMiddleware<span style="color: black;">{},</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>statusMw<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: black;">&</span>rest<span style="color: black;">.</span>StatusMiddleware<span style="color: black;">{}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>api<span style="color: black;">.</span>Use<span style="color: black;">(</span>statusMw<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>api<span style="color: black;">.</span>Use<span style="color: black;">(</span>MiddleWareStack<span style="color: black;">...)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>api<span style="color: black;">.</span>Use<span style="color: black;">(&</span>rest<span style="color: black;">.</span>IfMiddleware<span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Condition<span style="color: black;">:</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">func</span><span style="color: black;">(</span>request<span style="color: silver;"> </span><span style="color: black;">*</span>rest<span style="color: black;">.</span>Request<span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: navy;">bool</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>debugLevel<span style="color: silver;"> </span><span style="color: black;">></span><span style="color: silver;"> </span><span style="color: purple;">2</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Printf<span style="color: black;">(</span><span style="color: green;">"AUTH</span><span style="color: silver;"> </span><span style="color: green;">Request.URL.Path:</span><span style="color: silver;"> </span><span style="color: green;">'%s'</span><span style="color: silver;"> </span><span style="color: green;">returning</span><span style="color: silver;"> </span><span style="color: green;">'%b'\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>request<span style="color: black;">.</span>URL<span style="color: black;">.</span>Path<span style="color: black;">,</span><span style="color: silver;"> </span>request<span style="color: black;">.</span>URL<span style="color: black;">.</span>Path<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: green;">"/login"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>request<span style="color: black;">.</span>URL<span style="color: black;">.</span>Path<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: green;">"/login"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">},</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>IfTrue<span style="color: black;">:</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>jwt_middleware<span style="color: black;">,</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">})</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>router<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>rest<span style="color: black;">.</span>MakeRouter<span style="color: black;">(</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">JSON</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/j/t/:postid"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>JsonGetPostThreadComments<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/j/p/:orderby"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>JsonGetPosts<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">HTML</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>SendStaticMainHtml<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/t/:postid"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>SendStaticCommentsHtml<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Auth</span><span style="color: silver;"> </span><span style="color: green;">JWT</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Post<span style="color: black;">(</span><span style="color: green;">"/login"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>jwt_middleware<span style="color: black;">.</span>LoginHandler<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/jwttest"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>JwtTest<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Post<span style="color: black;">(</span><span style="color: green;">"/jwtposttest"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>JwtPostTest<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/refresh_token"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>jwt_middleware<span style="color: black;">.</span>RefreshHandler<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">JSON</span><span style="color: silver;"> </span><span style="color: green;">Depricated</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/p/:orderby"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>JsonGetAllPosts<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/p"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>JsonGetAllPosts<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">HTML,</span><span style="color: silver;"> </span><span style="color: green;">Images,</span><span style="color: silver;"> </span><span style="color: green;">CSS</span><span style="color: silver;"> </span><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">JS</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/img/#filename"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>SendStaticImage<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/css"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>SendStaticCss<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/css/#cssfile"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>SendStaticCss<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/js/#jsfile"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>SendStaticJS<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/js"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>SendStaticJS<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/html/*filename"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>GetHtmlFile<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/test/*filename"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">.</span>GetTestFile<span style="color: black;">),</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rest<span style="color: black;">.</span>Get<span style="color: black;">(</span><span style="color: green;">"/.status"</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">func</span><span style="color: black;">(</span>w<span style="color: silver;"> </span>rest<span style="color: black;">.</span>ResponseWriter<span style="color: black;">,</span><span style="color: silver;"> </span>r<span style="color: silver;"> </span><span style="color: black;">*</span>rest<span style="color: black;">.</span>Request<span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>w<span style="color: black;">.</span>WriteJson<span style="color: black;">(</span>statusMw<span style="color: black;">.</span>GetStatus<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}),</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>log<span style="color: black;">.</span>Fatal<span style="color: black;">(</span>err<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>api<span style="color: black;">.</span>SetApp<span style="color: black;">(</span>router<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//http.Handle("/static/",</span><span style="color: silver;"> </span><span style="color: green;">http.StripPrefix("/static",</span><span style="color: silver;"> </span><span style="color: green;">http.FileServer(http.Dir("."))))</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>http<span style="color: black;">.</span>Handle<span style="color: black;">(</span><span style="color: green;">"/api/"</span><span style="color: black;">,</span><span style="color: silver;"> </span>http<span style="color: black;">.</span>StripPrefix<span style="color: black;">(</span><span style="color: green;">"/api"</span><span style="color: black;">,</span><span style="color: silver;"> </span>api<span style="color: black;">.</span>MakeHandler<span style="color: black;">()))</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>http<span style="color: black;">.</span>Handle<span style="color: black;">(</span><span style="color: green;">"/"</span><span style="color: black;">,</span><span style="color: silver;"> </span>api<span style="color: black;">.</span>MakeHandler<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>debugLevel<span style="color: silver;"> </span><span style="color: black;">></span><span style="color: silver;"> </span><span style="color: purple;">2</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Println<span style="color: black;">(</span><span style="color: green;">"Starting</span><span style="color: silver;"> </span><span style="color: green;">http.ListenAndServe</span><span style="color: silver;"> </span><span style="color: green;">:8080"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//log.Fatal(http.ListenAndServe(":8080",</span><span style="color: silver;"> </span><span style="color: green;">api.MakeHandler()))</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>log<span style="color: black;">.</span>Fatal<span style="color: black;">(</span>http<span style="color: black;">.</span>ListenAndServe<span style="color: black;">(</span><span style="color: green;">":8080"</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: black;">))</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">--- demo --- </span></pre>
Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com0tag:blogger.com,1999:blog-3324173739769750106.post-71857533135470212662015-06-18T07:28:00.000+00:002015-06-19T15:00:34.153+00:00Day 17 - Using reflection to write into an interface slice<span style="font-size: large;">How to use reflection to write into a slice of unknown items inside an unknown struct:</span><br />
<br />
While working on <a href="https://github.com/kimxilxyong/gorp" target="_blank">Gorp with Indexes</a> i had to solve the problem of how to write into a slice inside a struct passed as an interface{}. A new struct with the filled slice should be returned. Both the struct and the slice are not known at compile time, i only have the fieldname of the slice passed to me at runtime. After some serious headscratching and reading through the reflection code i luckily found a way to do it.<br />
<br />
<br />
The basic method is:<br />
<ol>
<li>Convert the incoming interface i to a reflect.Type t</li>
<li>Dereference until we have a t which is a Struct</li>
<li>Create a new reflect.Value v from the Type t</li>
<li>From v we now can ask for the FieldName of the slice, getting back another reflect.Value s</li>
<li>As we now have the slice s as a reflect.Value, we need to get the slice elements type</li>
<li>From the slice elements type we create a new instance of it (reflect.New)</li>
<li>Write to fields in this newItem using FieldByName (Hardcoded in the example)</li>
<li>Append the newItem to the slice (the set append was the hard part to find out for me)</li>
<li>Return the reflect.Value v as an interface</li>
<li>???</li>
<li>Profit - Heureka!!</li>
</ol>
<br />
Output:<br />
<span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: white; color: black;">Input Type main.Post:</span></span><br />
<span style="font-family: "Courier New",Courier,monospace;">
</span><div style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
<span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: white; color: black;">Slice Type []*main.Comment:</span></span></div>
<span style="font-family: "Courier New",Courier,monospace;">
</span><div style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
<span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: white; color: black;">Slice Elem Type main.Comment:</span></span></div>
<span style="font-family: "Courier New",Courier,monospace;">
</span><div style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
<span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: white; color: black;">Comment 0, Body XYZ 0, PostId 0</span></span></div>
<span style="font-family: "Courier New",Courier,monospace;">
</span><div style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
<span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: white; color: black;">Comment 1, Body XYZ 1, PostId 2</span></span></div>
<span style="font-family: "Courier New",Courier,monospace;">
</span><div style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
<span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: white; color: black;">Comment 2, Body XYZ 2, PostId 4</span></span></div>
<span style="font-family: "Courier New",Courier,monospace;">
</span><div style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
<span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: white; color: black;">Comment 3, Body XYZ 3, PostId 6</span></span></div>
<span style="font-family: "Courier New",Courier,monospace;">
</span><div style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
<span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: white; color: black;">Comment 4, Body XYZ 4, PostId 8</span></span></div>
<span style="font-family: "Courier New",Courier,monospace;">
</span><div style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
<span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: white; color: navy; font-weight: 600;">Erfolg: Prozess beendet mit Rückgabewert 0.</span></span></div>
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">This</span><span style="color: silver;"> </span><span style="color: green;">is</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">demo</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">show</span><span style="color: silver;"> </span><span style="color: green;">how</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">convert</span><span style="color: silver;"> </span><span style="color: green;">from</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">normal</span><span style="color: silver;"> </span><span style="color: green;">struct</span></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">reflection</span><span style="color: silver;"> </span><span style="color: green;">type</span><span style="color: silver;"> </span><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">back</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">struct</span><span style="color: silver;"> </span><span style="color: green;">without</span><span style="color: silver;"> </span><span style="color: green;">knowing</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">original</span><span style="color: silver;"> </span><span style="color: green;">one.</span><span style="color: silver;"> </span><span style="color: green;">Input is</span><span style="color: silver;"> </span><span style="color: green;">passed</span><span style="color: silver;"> </span><span style="color: green;">as</span><span style="color: silver;"> </span><span style="color: green;">an</span><span style="color: silver;"> </span><span style="color: green;">Interface</span><span style="color: silver;"> </span><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">// output</span> <span style="color: green;">will</span><span style="color: silver;"> </span><span style="color: green;">be</span><span style="color: silver;"> </span><span style="color: green;">an</span><span style="color: silver;"> </span><span style="color: green;">interface,</span><span style="color: silver;"> </span><span style="color: green;">too.</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Bonus</span><span style="color: silver;"> </span><span style="color: green;">points</span><span style="color: silver;"> </span><span style="color: green;">for</span><span style="color: silver;"> </span><span style="color: green;">writing</span><span style="color: silver;"> </span><span style="color: green;">into</span><span style="color: silver;"> </span><span style="color: green;">an</span><span style="color: silver;"> </span><span style="color: green;">embedded</span><span style="color: silver;"> </span><span style="color: green;">slice</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">(=</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">embedded</span><span style="color: silver;"> </span><span style="color: green;">Comment</span><span style="color: silver;"> </span><span style="color: green;">struct</span><span style="color: silver;"> </span><span style="color: green;">slice in Post)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">package</span><span style="color: silver;"> </span>main</pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">import</span><span style="color: silver;"> </span><span style="color: black;">(</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"errors"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"fmt"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"os"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"reflect"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">type</span><span style="color: silver;"> </span><span style="color: navy;">Post</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">struct</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Id<span style="color: silver;"> </span><span style="color: navy;">uint64</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Title<span style="color: silver;"> </span><span style="color: navy;">string</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Comments<span style="color: silver;"> </span><span style="color: black;">[]*</span>Comment</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">holds</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">single</span><span style="color: silver;"> </span><span style="color: green;">comment</span><span style="color: silver;"> </span><span style="color: green;">bound</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">post</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">type</span><span style="color: silver;"> </span><span style="color: navy;">Comment</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">struct</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Id<span style="color: silver;"> </span><span style="color: navy;">uint64</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>PostId<span style="color: silver;"> </span><span style="color: navy;">uint64</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Body<span style="color: silver;"> </span><span style="color: navy;">string</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">CreateAndFillSlice(</span>i<span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">interface</span><span style="color: black;">{},</span><span style="color: silver;"> </span>sliceName<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">(</span><span style="color: navy; font-weight: 600;">interface</span><span style="color: black;">{},</span><span style="color: silver;"> </span><span style="color: navy;">error</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Convert</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">interface</span><span style="color: silver;"> </span><span style="color: green;">i</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">reflect.Type</span><span style="color: silver;"> </span><span style="color: green;">t</span> </pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"> t<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>reflect<span style="color: black;">.</span>TypeOf<span style="color: black;">(</span>i<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Check</span><span style="color: silver;"> </span><span style="color: green;">if</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">input</span><span style="color: silver;"> </span><span style="color: green;">is</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">pointer</span><span style="color: silver;"> </span><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">dereference</span><span style="color: silver;"> </span><span style="color: green;">it</span><span style="color: silver;"> </span><span style="color: green;">if</span><span style="color: silver;"> </span><span style="color: green;">yes</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>t<span style="color: black;">.</span>Kind<span style="color: black;">()</span><span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span>reflect<span style="color: black;">.</span>Ptr<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>t<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>t<span style="color: black;">.</span>Elem<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Check</span><span style="color: silver;"> </span><span style="color: green;">if</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">input</span><span style="color: silver;"> </span><span style="color: green;">is</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">struct</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>t<span style="color: black;">.</span>Kind<span style="color: black;">()</span><span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span>reflect<span style="color: black;">.</span>Struct<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: black;">,</span><span style="color: silver;"> </span>errors<span style="color: black;">.</span>New<span style="color: black;">(</span><span style="color: green;">"Input</span><span style="color: silver;"> </span><span style="color: green;">param</span><span style="color: silver;"> </span><span style="color: green;">is</span><span style="color: silver;"> </span><span style="color: green;">not</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">struct"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Printf<span style="color: black;">(</span><span style="color: green;">"Input</span><span style="color: silver;"> </span><span style="color: green;">Type</span><span style="color: silver;"> </span><span style="color: green;">%v:\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>t<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Create</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">new</span><span style="color: silver;"> </span><span style="color: green;">Value</span><span style="color: silver;"> </span><span style="color: green;">from</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">input</span><span style="color: silver;"> </span><span style="color: green;">type</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">this</span><span style="color: silver;"> </span><span style="color: green;">will</span><span style="color: silver;"> </span><span style="color: green;">be</span><span style="color: silver;"> </span><span style="color: green;">returned</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">caller</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>v<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>reflect<span style="color: black;">.</span>New<span style="color: black;">(</span>t<span style="color: black;">).</span>Elem<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Get</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">field</span><span style="color: silver;"> </span><span style="color: green;">named</span><span style="color: silver;"> </span><span style="color: green;">"sliceName"</span><span style="color: silver;"> </span><span style="color: green;">from</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">input</span><span style="color: silver;"> </span><span style="color: green;">struct,</span><span style="color: silver;"> </span><span style="color: green;">which</span><span style="color: silver;"> </span><span style="color: green;">should</span><span style="color: silver;"> </span><span style="color: green;">be</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">slice</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>s<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>v<span style="color: black;">.</span>FieldByName<span style="color: black;">(</span>sliceName<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>s<span style="color: black;">.</span>Kind<span style="color: black;">()</span><span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span>reflect<span style="color: black;">.</span>Slice<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>st<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>s<span style="color: black;">.</span>Type<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Printf<span style="color: black;">(</span><span style="color: green;">"Slice</span><span style="color: silver;"> </span><span style="color: green;">Type</span><span style="color: silver;"> </span><span style="color: green;">%s:\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>st<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Get</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">type</span><span style="color: silver;"> </span><span style="color: green;">of</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">single</span><span style="color: silver;"> </span><span style="color: green;">slice</span><span style="color: silver;"> </span><span style="color: green;">element</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>sliceType<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>st<span style="color: black;">.</span>Elem<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Pointer?</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>sliceType<span style="color: black;">.</span>Kind<span style="color: black;">()</span><span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span>reflect<span style="color: black;">.</span>Ptr<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Then</span><span style="color: silver;"> </span><span style="color: green;">dereference</span><span style="color: silver;"> </span><span style="color: green;">it</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>sliceType<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>sliceType<span style="color: black;">.</span>Elem<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Printf<span style="color: black;">(</span><span style="color: green;">"Slice</span><span style="color: silver;"> </span><span style="color: green;">Elem</span><span style="color: silver;"> </span><span style="color: green;">Type</span><span style="color: silver;"> </span><span style="color: green;">%v:\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>sliceType<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">for</span><span style="color: silver;"> </span>i<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: purple;">0</span><span style="color: black;">;</span><span style="color: silver;"> </span>i<span style="color: silver;"> </span><span style="color: black;"><</span><span style="color: silver;"> </span><span style="color: purple;">5</span><span style="color: black;">;</span><span style="color: silver;"> </span>i<span style="color: black;">++</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Create</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">new</span><span style="color: silver;"> </span><span style="color: green;">slice</span><span style="color: silver;"> </span><span style="color: green;">element</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>newitem<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>reflect<span style="color: black;">.</span>New<span style="color: black;">(</span>sliceType<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Set</span><span style="color: silver;"> </span><span style="color: green;">some</span><span style="color: silver;"> </span><span style="color: green;">field</span><span style="color: silver;"> </span><span style="color: green;">in</span><span style="color: silver;"> </span><span style="color: green;">it</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>newitem<span style="color: black;">.</span>Elem<span style="color: black;">().</span>FieldByName<span style="color: black;">(</span><span style="color: green;">"Body"</span><span style="color: black;">).</span>SetString<span style="color: black;">(</span>fmt<span style="color: black;">.</span>Sprintf<span style="color: black;">(</span><span style="color: green;">"XYZ</span><span style="color: silver;"> </span><span style="color: green;">%d"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">))</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>newitem<span style="color: black;">.</span>Elem<span style="color: black;">().</span>FieldByName<span style="color: black;">(</span><span style="color: green;">"PostId"</span><span style="color: black;">).</span>SetUint<span style="color: black;">(</span><span style="color: navy;">uint64</span><span style="color: black;">(</span>i<span style="color: silver;"> </span><span style="color: black;">*</span><span style="color: silver;"> </span><span style="color: purple;">2</span><span style="color: black;">))</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">This</span><span style="color: silver;"> </span><span style="color: green;">is</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">important</span><span style="color: silver;"> </span><span style="color: green;">part</span><span style="color: silver;"> </span><span style="color: green;">here</span><span style="color: silver;"> </span><span style="color: green;">-</span><span style="color: silver;"> </span><span style="color: green;">append</span><span style="color: silver;"> </span><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">set</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Append</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">newitem</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">slice</span><span style="color: silver;"> </span><span style="color: green;">in</span><span style="color: silver;"> </span><span style="color: green;">"v"</span><span style="color: silver;"> </span><span style="color: green;">which</span><span style="color: silver;"> </span><span style="color: green;">will</span><span style="color: silver;"> </span><span style="color: green;">be</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">output</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>s<span style="color: black;">.</span>Set<span style="color: black;">(</span>reflect<span style="color: black;">.</span>Append<span style="color: black;">(</span>s<span style="color: black;">,</span><span style="color: silver;"> </span>newitem<span style="color: black;">))</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">else</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: black;">,</span><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Errorf<span style="color: black;">(</span><span style="color: green;">"Field</span><span style="color: silver;"> </span><span style="color: green;">%s</span><span style="color: silver;"> </span><span style="color: green;">is</span><span style="color: silver;"> </span><span style="color: green;">not</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">slice\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>sliceName<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">IMPORTANT</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Cast</span><span style="color: silver;"> </span><span style="color: green;">back</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">empty</span><span style="color: silver;"> </span><span style="color: green;">interface</span><span style="color: silver;"> </span><span style="color: green;">type</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">So</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">cast</span><span style="color: silver;"> </span><span style="color: green;">back</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">Post</span><span style="color: silver;"> </span><span style="color: green;">outside</span><span style="color: silver;"> </span><span style="color: green;">will</span><span style="color: silver;"> </span><span style="color: green;">work</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>v<span style="color: black;">.</span>Interface<span style="color: black;">(),</span><span style="color: silver;"> </span><span style="color: navy;">nil</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">main()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">var</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: navy;">error</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>p<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>Post<span style="color: black;">{</span>Id<span style="color: black;">:</span><span style="color: silver;"> </span><span style="color: purple;">1</span><span style="color: black;">,</span><span style="color: silver;"> </span>Title<span style="color: black;">:</span><span style="color: silver;"> </span><span style="color: green;">"Title</span><span style="color: silver;"> </span><span style="color: green;">1"</span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>result<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>CreateAndFillSlice<span style="color: black;">(</span>p<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"Comments"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Println<span style="color: black;">(</span>err<span style="color: black;">.</span>Error<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>os<span style="color: black;">.</span>Exit<span style="color: black;">(</span><span style="color: purple;">1</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Cast</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">returned</span><span style="color: silver;"> </span><span style="color: green;">interface</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">Post</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>post<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>result<span style="color: black;">.(</span>Post<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">for</span><span style="color: silver;"> </span>i<span style="color: black;">,</span><span style="color: silver;"> </span>c<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">range</span><span style="color: silver;"> </span>post<span style="color: black;">.</span>Comments<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Printf<span style="color: black;">(</span><span style="color: green;">"Comment</span><span style="color: silver;"> </span><span style="color: green;">%d,</span><span style="color: silver;"> </span><span style="color: green;">Body</span><span style="color: silver;"> </span><span style="color: green;">%s,</span><span style="color: silver;"> </span><span style="color: green;">PostId</span><span style="color: silver;"> </span><span style="color: green;">%d\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>i<span style="color: black;">,</span><span style="color: silver;"> </span>c<span style="color: black;">.</span>Body<span style="color: black;">,</span><span style="color: silver;"> </span>c<span style="color: black;">.</span>PostId<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com4tag:blogger.com,1999:blog-3324173739769750106.post-61742876053226109852015-06-06T00:42:00.002+00:002015-06-18T03:15:36.053+00:00 Day 16 - Comparison of html parsers for a webcrawler, today GoQuery<a href="http://github.com/PuerkitoBio/goquery" target="_blank">github.com/goquery</a> is the king of go html parsing. After trying the two other<br />
relevant libs i can conclude:<br />
<br />
<ul>
<li>GoQuery: All you want, you can use it as a Ferrari or as a heavy load truck</li>
</ul>
<ul>
<li><a href="http://intogooglego.blogspot.co.at/2015/06/day-13-comparison-of-html-parsers-for.html" target="_blank">Scrape</a>: Small, light and neat: Its your bicycle, it always works</li>
</ul>
<ul>
<li><a href="http://intogooglego.blogspot.co.at/2015/06/day-15-comparison-of-html-scrapers.html" target="_blank">go-html-transform</a>: For me it feels like an energy plant with all buttons in "<span class="short_text" id="result_box" lang="ru"><span class="hps">русский</span> <span class="hps">этикетки"</span></span></li>
</ul>
<br />
<br />
This example to scrape posts from <a href="https://news.ycombinator.com/newest" target="_blank">hackernews</a> has been stripped down to<br />
fit onto one page. All error handling and debug printouts have been<br />
removed, so only the pure GoQuery logic remains.<br />
The full source is available at <a href="https://github.com/kimxilxyong/intogooglego/tree/master/hackernewsCrawlerGoQuery" target="_blank">github.com/hackernewsCrawlerGoQuery</a>.<br />
<br />
<div style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
<br /></div>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Parse</span><span style="color: silver;"> </span><span style="color: green;">for</span><span style="color: silver;"> </span><span style="color: green;">posts</span><span style="color: silver;"> </span><span style="color: green;">in</span><span style="color: silver;"> </span><span style="color: green;">html</span><span style="color: silver;"> </span><span style="color: green;">from</span><span style="color: silver;"> </span><span style="color: green;">hackernews,</span><span style="color: silver;"> </span><span style="color: green;">input</span><span style="color: silver;"> </span><span style="color: green;">html</span><span style="color: silver;"> </span><span style="color: green;">is</span><span style="color: silver;"> </span><span style="color: green;">an</span><span style="color: silver;"> </span><span style="color: green;">io.Reader</span><span style="color: silver;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;"><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">returns</span> recognized</span><span style="color: silver;"> </span><span style="color: green;">posts</span><span style="color: silver;"> </span><span style="color: green;">in</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">psout</span><span style="color: silver;"> </span><span style="color: green;">slice</span><span style="color: silver;"> </span><span style="color: green;">of</span><span style="color: silver;"> </span><span style="color: green;">posts.</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Errors</span><span style="color: silver;"> </span><span style="color: green;">which</span><span style="color: silver;"> </span><span style="color: green;">affect</span><span style="color: silver;"> </span><span style="color: green;">only</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">single</span><span style="color: silver;"> </span><span style="color: green;">post</span><span style="color: silver;"> </span><span style="color: green;">are</span><span style="color: silver;"> </span><span style="color: green;">stored</span><span style="color: silver;"> </span><span style="color: green;">in</span><span style="color: silver;"> </span><span style="color: green;">their</span><span style="color: silver;"> </span><span style="color: green;">post.Err</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">ParseHtmlHackerNews(</span>body<span style="color: silver;"> </span>io<span style="color: black;">.</span>Reader<span style="color: black;">,</span><span style="color: silver;"> </span>ps<span style="color: silver;"> </span><span style="color: black;">[]*</span>post<span style="color: black;">.</span>Post<span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"><span style="color: black;"> (</span>psout<span style="color: silver;"> </span><span style="color: black;">[]*</span>post<span style="color: black;">.</span>Post<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: navy;">error</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Create</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">qoquery</span><span style="color: silver;"> </span><span style="color: green;">document</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">parse</span><span style="color: silver;"> </span><span style="color: green;">from</span><span style="color: silver;"> </span><span style="color: green;">an</span><span style="color: silver;"> </span><span style="color: green;">io.Reader </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>doc<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>goquery<span style="color: black;">.</span>NewDocumentFromReader<span style="color: black;">(</span>body<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Find</span><span style="color: silver;"> </span><span style="color: green;">hackernews</span><span style="color: silver;"> </span><span style="color: green;">posts</span><span style="color: silver;"> </span><span style="color: green;">=</span><span style="color: silver;"> </span><span style="color: green;">elements</span><span style="color: silver;"> </span><span style="color: green;">with</span><span style="color: silver;"> </span><span style="color: green;">class</span><span style="color: silver;"> </span><span style="color: green;">"athing"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>thing<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>doc<span style="color: black;">.</span>Find<span style="color: black;">(</span><span style="color: green;">".athing"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">for</span><span style="color: silver;"> </span>iThing<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">range</span><span style="color: silver;"> </span>thing<span style="color: black;">.</span>Nodes<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Create</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">new</span><span style="color: silver;"> </span><span style="color: green;">post</span><span style="color: silver;"> </span><span style="color: green;">struct</span><span style="color: silver;"> </span><span style="color: green;">-</span><span style="color: silver;"> </span><span style="color: green;">if</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">crawling</span><span style="color: silver;"> </span><span style="color: green;">fails</span><span style="color: silver;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">post</span><span style="color: silver;"> </span><span style="color: green;">will</span><span style="color: silver;"> </span><span style="color: green;">have</span><span style="color: silver;"> </span><span style="color: green;">its</span><span style="color: silver;"> </span><span style="color: green;">Err</span><span style="color: silver;"> </span><span style="color: green;">set,</span><span style="color: silver;"> </span><span style="color: green;">but</span><span style="color: silver;"> </span><span style="color: green;">will</span><span style="color: silver;"> </span><span style="color: green;">be</span><span style="color: silver;"> </span><span style="color: green;">added</span><span style="color: silver;"> </span><span style="color: green;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;"> // </span><span style="color: green;"><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">outgoing</span><span style="color: silver;"> </span><span style="color: green;">(psout)</span><span style="color: silver;"> </span><span style="color: green;">slice</span><span style="color: silver;"> </span>nevertheless</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>post<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>post<span style="color: black;">.</span>NewPost<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>ps<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span><span style="color: navy;">append</span><span style="color: black;">(</span>ps<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: black;">&</span>post<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">use</span><span style="color: silver;"> </span><span style="color: green;">singlearticle as</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">selection</span><span style="color: silver;"> </span><span style="color: green;">of</span><span style="color: silver;"> </span><span style="color: green;">one</span><span style="color: silver;"> </span><span style="color: green;">single</span><span style="color: silver;"> </span><span style="color: green;">post</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>singlearticle<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>thing<span style="color: black;">.</span>Eq<span style="color: black;">(</span>iThing<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Get</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">next</span><span style="color: silver;"> </span><span style="color: green;">element</span><span style="color: silver;"> </span><span style="color: green;">containing</span><span style="color: silver;"> </span><span style="color: green;">additional</span><span style="color: silver;"> </span><span style="color: green;">info</span><span style="color: silver;"> </span><span style="color: green;">for</span><span style="color: silver;"> </span><span style="color: green;">this</span><span style="color: silver;"> </span><span style="color: green;">post</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>scorenode<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>singlearticle<span style="color: black;">.</span>Next<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Get</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">post</span><span style="color: silver;"> </span><span style="color: green;">title</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>htmlpost<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>singlearticle<span style="color: black;">.</span>Find<span style="color: black;">(</span><span style="color: green;">".title</span><span style="color: silver;"> </span><span style="color: green;">a"</span><span style="color: black;">).</span>First<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>post<span style="color: black;">.</span>Title<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>htmlpost<span style="color: black;">.</span>Text<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Get</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">post</span><span style="color: silver;"> </span><span style="color: green;">url</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>post<span style="color: black;">.</span>Url<span style="color: black;">,</span><span style="color: silver;"> </span>exists<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>htmlpost<span style="color: black;">.</span>Attr<span style="color: black;">(</span><span style="color: green;">"href"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Get</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">post</span><span style="color: silver;"> </span><span style="color: green;">score</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>scoretag<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>scorenode<span style="color: black;">.</span>Find<span style="color: black;">(</span><span style="color: green;">".subtext</span><span style="color: silver;"> </span><span style="color: green;">.score"</span><span style="color: black;">).</span>First<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>post<span style="color: black;">.</span>SetScore<span style="color: black;">(</span>strings<span style="color: black;">.</span>Split<span style="color: black;">(</span>scoretag<span style="color: black;">.</span>Text<span style="color: black;">(),</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: black;">)[</span><span style="color: purple;">0</span><span style="color: black;">])</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Get</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">post</span><span style="color: silver;"> </span><span style="color: green;">id</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>postid<span style="color: black;">,</span><span style="color: silver;"> </span>exists<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>scoretag<span style="color: black;">.</span>Attr<span style="color: black;">(</span><span style="color: green;">"id"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>post<span style="color: black;">.</span>PostId<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>strings<span style="color: black;">.</span>Split<span style="color: black;">(</span>postid<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"_"</span><span style="color: black;">)[</span><span style="color: purple;">1</span><span style="color: black;">]</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Get</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">username</span><span style="color: silver;"> </span><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">postdate</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>hrefs<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>scorenode<span style="color: black;">.</span>Find<span style="color: black;">(</span><span style="color: green;">".subtext</span><span style="color: silver;"> </span><span style="color: green;">a"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">for</span><span style="color: silver;"> </span>i<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">range</span><span style="color: silver;"> </span>hrefs<span style="color: black;">.</span>Nodes<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>href<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>hrefs<span style="color: black;">.</span>Eq<span style="color: black;">(</span>i<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>t<span style="color: black;">,</span><span style="color: silver;"> </span>_<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>href<span style="color: black;">.</span>Html<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>s<span style="color: black;">,</span><span style="color: silver;"> </span>exists<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>href<span style="color: black;">.</span>Attr<span style="color: black;">(</span><span style="color: green;">"href"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>exists<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>strings<span style="color: black;">.</span>HasPrefix<span style="color: black;">(</span>s<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"user?id"</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>post<span style="color: black;">.</span>User<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>t</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">continue</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>strings<span style="color: black;">.</span>HasPrefix<span style="color: black;">(</span>s<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"item?id"</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>strings<span style="color: black;">.</span>Contains<span style="color: black;">(</span>t<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"ago"</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">var</span><span style="color: silver;"> </span>postDate<span style="color: silver;"> </span>time<span style="color: black;">.</span>Time</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>postDate<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>GetDateFromCreatedAgo<span style="color: black;">(</span>t<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>post<span style="color: black;">.</span>PostDate<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>postDate</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>post<span style="color: black;">.</span>Err<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>ps<span style="color: black;">,</span><span style="color: silver;"> </span>err</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com1tag:blogger.com,1999:blog-3324173739769750106.post-64764170217305464432015-06-05T12:02:00.001+00:002015-06-06T01:07:01.800+00:00Day 15 - Comparison of html parsers for a webcrawler, today go-html-transform<a href="http://bitbucket.org/zaphar/go-html-transform" target="_blank">http://bitbucket.org/zaphar/go-html-transform</a> looks very promising and powerfull, as its not only a scraping lib but is able to manipulate the DOM.<br />
<br />
I was really excited to get my hands onto it: It should be providing a simple traversing walker (like <a href="http://github.com/yhat/scrape" target="_blank">scrape</a>). So i wrote a main loop to get articles from hackernews (<span style="color: green;">"http://news.ycombinator.com/"</span>)<br />
<br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>tree<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>h5<span style="color: black;">.</span>New<span style="color: black;">(</span>body<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>errors<span style="color: black;">.</span>New<span style="color: black;">(</span><span style="color: green;">"Failed</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">html.Parse:</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>err<span style="color: black;">.</span>Error<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>matched<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: black;">[]*</span>html<span style="color: black;">.</span>Node<span style="color: black;">{}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"><span style="color: green;"> //</span><span style="color: silver;"> </span><span style="color: green;">Get</span><span style="color: silver;"> </span><span style="color: green;">all</span><span style="color: silver;"> </span><span style="color: green;">articles</span></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>tree<span style="color: black;">.</span>Walk<span style="color: black;">(</span><span style="color: navy; font-weight: 600;">func</span><span style="color: black;">(</span>n<span style="color: silver;"> </span><span style="color: black;">*</span>html<span style="color: black;">.</span>Node<span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">check</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">node</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>n<span style="color: black;">.</span>DataAtom<span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span>atom<span style="color: black;">.</span>Tr<span style="color: silver;"> </span><span style="color: black;">&&</span><span style="color: silver;"> </span>n<span style="color: black;">.</span>Parent<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">&&</span><span style="color: silver;"> </span> </pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"> n<span style="color: black;">.</span>Parent<span style="color: black;">.</span>DataAtom<span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span>atom<span style="color: black;">.</span>Tbody<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">for</span><span style="color: silver;"> </span>_<span style="color: black;">,</span><span style="color: silver;"> </span>a<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">range</span><span style="color: silver;"> </span>n<span style="color: black;">.</span>Attr<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>a<span style="color: black;">.</span>Key<span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span><span style="color: green;">"class"</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>a<span style="color: black;">.</span>Val<span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span><span style="color: green;">"athing"</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>matched<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span><span style="color: navy;">append</span><span style="color: black;">(</span>matched<span style="color: black;">,</span><span style="color: silver;"> </span>n<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">})</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
Ok, thats a little bit more code then using <a href="https://github.com/yhat/scrape" target="_blank">scrape</a> but im sure it will pay out sometime later.<br />
And I found out that it supports CSS3 selectors - Great! - so whats that?<br />
<br />
Its like a query language, you can request a class with ".", an id with "#", and so on (<a href="http://www.w3schools.com/cssref/css_selectors.asp" target="_blank">.w3schools.com/cssref/css_selectors</a>) <br />
<br />
Great, that is just what i want - the lack of documentation doesnt scare me off.<br />
<br />
What then throw me off was not only the lack of examples, but the fact that the projects migration from <a href="https://code.google.com/p/go-html-transform/" target="_blank">code.google.com</a> to <a href="https://bitbucket.org/zaphar/go-html-transform" target="_blank">bitbucket</a> left some dangling imports like:<br />
<br />
<span style="color: green;"></span>
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">The</span><span style="color: silver;"> </span><span style="color: green;">package</span><span style="color: silver;"> </span><span style="color: green;">follows</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">CSS3</span><span style="color: silver;"> </span><span style="color: green;">Spec</span><span style="color: silver;"> </span><span style="color: green;">at:</span><span style="color: silver;"> </span><span style="color: green;">http://www.w3.org/TR/css3-selectors/</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">package</span><span style="color: silver;"> </span>selector</pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">import</span><span style="color: silver;"> </span><span style="color: black;">(</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"go.marzhillstudios.com/pkg/go-html-transform/h5"</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<span style="color: green;"></span><br />
go.marzhillstudios.com seems to be dead, no response, tcp timeout. So i do not want to base a project on a dead lib, even if it looks to be the best!.<br />
Its just too much effort to get it running and hoping it will not be disbanded. You are much better of using <a href="https://github.com/yhat/scrape" target="_blank">scrape</a> for a simple to use interface, or <a href="https://github.com/PuerkitoBio/goquery" target="_blank">goquery</a> for a fully fledged jquery compatible selector lib.<br />
<br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com0tag:blogger.com,1999:blog-3324173739769750106.post-24160510591802118292015-06-04T04:45:00.003+00:002015-06-06T06:57:33.307+00:00Day 14 - Comparison of html parsers for a webcrawler, today scrape<span style="font-size: small;">Scrape (<a href="http://github.com/yhat/scrape" target="_blank">github.com/yhat/scrape</a>) is a lightweight layer above <a href="http://godoc.org/golang.org/x/net/html" target="_blank">godoc.org/golang.org/x/net/html</a>. It makes it very easy to traverse the <span style="font-family: "Courier New",Courier,monospace;"><span style="color: black;">[]*</span>html<span style="color: black;">.</span>Node</span> tree and provides some convenience functions such as <code>Attr</code> and <code>Text</code>.</span><br />
<br />
After grokking the concept its super easy to use and get fast results.<br />
<br />
There are really only two things to understand:<br />
<br />
1) The traversing functions Find and FindAll:<br />
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"></span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">starts to search the node tree beginning at node article</span><span style="color: green;"><span style="color: black;"> (*</span>html<span style="color: black;">.</span>Node)</span></pre>
<span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;">titlenode<span style="color: black;">,</span><span style="color: silver;"> </span>ok<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>scrape<span style="color: black;">.</span>Find<span style="color: black;">(</span>article<span style="color: black;">,</span></span></span>
<br />
<span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">func</span><span style="color: black;">(</span>n<span style="color: silver;"> </span><span style="color: black;">*</span>html<span style="color: black;">.</span><span style="color: navy;">Node</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: navy;">bool</span><span style="color: silver;"> </span><span style="color: black;">{</span>
</span></span><br />
<span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>n<span style="color: black;">.</span>DataAtom<span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span>atom<span style="color: black;">.</span>Td<span style="color: silver;"> </span><span style="color: silver;"></span><span style="color: black;">&&</span><span style="color: silver;"> </span>scrape<span style="color: black;">.</span>Attr<span style="color: black;">(</span>n<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"class"</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span><span style="color: green;">"title"</span><span style="color: silver;"> </span><span style="color: black;">{</span>
</span></span><br />
<span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span><span style="color: navy;">true</span>
</span></span><br />
<span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;"><span style="color: silver;"> </span><span style="color: black;">}</span>
</span></span><br />
<span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span><span style="color: navy;">false</span>
</span></span><br />
<span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;"><span style="color: silver;"> </span><span style="color: black;">})</span>
</span></span><br />
<span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span><span style="color: black;">!</span>ok<span style="color: silver;"> </span><span style="color: black;">{</span>
</span></span><br />
<span style="font-size: small;"><span style="color: silver;"><span style="font-family: "Courier New",Courier,monospace;"> </span></span></span>... do some error<br />
<div style="margin: 0px; text-indent: 0px;">
<span style="font-size: small;"><span style="color: silver;"> </span><span style="color: black;">}</span></span></div>
<br />
<br />
2) The matcher function which controls which nodes will be returned:<br />
A matcher function is an argument to Find or Findall, gets a Node as an input param and returns ether false or true. If true the node n is included in the result of FindAll. In the case of Find a true result from the matcher causes Find to stop and return the Node n. <br />
<style type="text/css">
p, li { white-space: pre-wra</style>
<br />
<div style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
<br /></div>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">define</span><span style="color: silver;"> </span><span style="color: green;">a matcher </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>matcher<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">func</span><span style="color: black;">(</span>n<span style="color: silver;"> </span><span style="color: black;">*</span>html<span style="color: black;">.</span>Node<span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: navy;">bool</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>n<span style="color: black;">.</span>DataAtom<span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span>atom<span style="color: black;">.</span>Tr<span style="color: silver;"> </span><span style="color: black;">&&</span><span style="color: silver;"> </span>n<span style="color: black;">.</span>Parent<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">&&</span><span style="color: silver;"> </span>n<span style="color: black;">.</span>Parent<span style="color: black;">.</span>DataAtom<span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span>atom<span style="color: black;">.</span>Tbody<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>matched<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>scrape<span style="color: black;">.</span>Attr<span style="color: black;">(</span>n<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"class"</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span><span style="color: green;">"athing"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>matched</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span><span style="color: navy;">false</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<br />
<br />
<br />
The full example code for this blog entry can be found at <a href="https://github.com/kimxilxyong/intogooglego/tree/master/hackernewsCrawlerScrape" target="_blank">github.com/kimxilxyong/intogooglego/hackernewsCrawlerScrape</a><br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com1tag:blogger.com,1999:blog-3324173739769750106.post-89334237476693003692015-05-30T05:29:00.000+00:002015-06-02T08:38:12.109+00:00Day 13 - Howto use gorp selectThe <span style="font-family: "Courier New",Courier,monospace;">dbmap<span style="color: black;">.</span>Select</span> function in gorp can be used in two different ways. .<br />
<br />
<span style="color: black;"></span>
<br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Method</span><span style="color: silver;"> </span><span style="color: green;">1:</span><span style="color: silver;"> </span><span style="color: green;">Results</span><span style="color: silver;"> </span><span style="color: green;">are</span><span style="color: silver;"> </span><span style="color: green;">returned</span><span style="color: silver;"> </span><span style="color: green;">as</span><span style="color: silver;"> </span><span style="color: green;">an</span><span style="color: silver;"> </span><span style="color: green;">array</span><span style="color: silver;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">of</span><span style="color: silver;"> </span><span style="color: green;">interfaces</span><span style="color: silver;"> </span><span style="color: green;">( = rows</span><span style="color: silver;"> </span><span style="color: green;">here )</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rows<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>dbmap<span style="color: black;">.</span>Select<span style="color: black;">(</span>foo<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"select</span><span style="color: silver;"> </span><span style="color: green;">*</span><span style="color: silver;"> </span><span style="color: green;">from</span><span style="color: silver;"> </span><span style="color: green;">" </span><span style="color: black;">+ </span>table<span style="color: black;">.</span>TableName<span style="color: black;">)</span></pre>
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Method</span><span style="color: silver;"> </span><span style="color: green;">1:</span><span style="color: silver;"> </span><span style="color: green;">read</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">rows</span><span style="color: silver;"> </span><span style="color: green;">from</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">returned</span><span style="color: silver;"> </span><span style="color: green;">array</span><span style="color: silver;"> </span><span style="color: green;">of</span><span style="color: silver;"> </span><span style="color: green;">interfaces</span><span style="color: silver;"> </span><span style="color: green;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;"> // </span><span style="color: silver;"></span><span style="color: green;">rows</span><span style="color: silver;"> </span><span style="color: green;">:=</span><span style="color: silver;"> </span><span style="color: green;">[]interface{}</span><span style="color: silver;"></span><span style="color: green;"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">for</span><span style="color: silver;"> </span>_<span style="color: black;">,</span><span style="color: silver;"> </span>row<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">range</span><span style="color: silver;"> </span>rows<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">cast</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">row</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">our</span><span style="color: silver;"> </span><span style="color: green;">struct</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>af<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>row<span style="color: black;">.(*</span>AliasTransientField<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Printf<span style="color: black;">(</span><span style="color: green;">"Method1:</span><span style="color: silver;"> </span><span style="color: green;">ID:</span><span style="color: silver;"> </span><span style="color: green;">%d,</span><span style="color: silver;"> </span><span style="color: green;">BarStr:</span><span style="color: silver;"> </span><span style="color: green;">%s\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>af<span style="color: black;">.</span>GetId<span style="color: black;">(),</span><span style="color: silver;"> </span>af<span style="color: black;">.</span>BarStr<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Method</span><span style="color: silver;"> </span><span style="color: green;">2:</span><span style="color: silver;"> </span><span style="color: green;">Resulting</span><span style="color: silver;"> </span><span style="color: green;">rows</span><span style="color: silver;"> </span><span style="color: green;">are</span><span style="color: silver;"> </span><span style="color: green;">appended</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">pointer</span><span style="color: silver;"> </span><span style="color: green;">of</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">slice</span><span style="color: silver;"> </span><span style="color: green;">(foos)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">var</span><span style="color: silver;"> </span>foos<span style="color: silver;"> </span><span style="color: black;">[]</span>AliasTransientField</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>_<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>dbmap<span style="color: black;">.</span>Select<span style="color: black;">(&</span>foos<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"select</span><span style="color: silver;"> </span><span style="color: green;">*</span><span style="color: silver;"> </span><span style="color: green;">from</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: black;"> + </span>table<span style="color: black;">.</span>TableName<span style="color: black;">)</span></pre>
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Method</span><span style="color: silver;"> </span><span style="color: green;">2:</span><span style="color: silver;"> </span><span style="color: green;">read</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">rows</span><span style="color: silver;"> </span><span style="color: green;">from</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">input</span><span style="color: silver;"> </span><span style="color: green;">slice</span><span style="color: silver;"> </span><span style="color: green;">(=foos)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">for</span><span style="color: silver;"> </span>_<span style="color: black;">,</span><span style="color: silver;"> </span>f<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">range</span><span style="color: silver;"> </span>foos<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Printf<span style="color: black;">(</span><span style="color: green;">"Method2:</span><span style="color: silver;"> </span><span style="color: green;">ID:</span><span style="color: silver;"> </span><span style="color: green;">%d,</span><span style="color: silver;"> </span><span style="color: green;">BarStr:</span><span style="color: silver;"> </span><span style="color: green;">%s\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>f<span style="color: black;">.</span>Id<span style="color: black;">,</span><span style="color: silver;"> </span>f<span style="color: black;">.</span>BarStr<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<br />
I needed some time to figure this out (especially the cast) and I hope it can be useful to somebody.<br />
<br />
The full code for this test is in: <a href="https://github.com/kimxilxyong/intogooglego/tree/master/testgorp" target="_blank">https://github.com/kimxilxyong/intogooglego/tree/master/testgorp</a> <br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com0tag:blogger.com,1999:blog-3324173739769750106.post-32987622279886857662015-05-29T09:14:00.001+00:002015-05-30T00:47:30.495+00:00Day 12 - Gorp with Indexes is now ready for beta tests<a href="https://github.com/kimxilxyong/gorp" target="_blank">Gorp with Indexes</a> now passes the test cases for MySQL and PostgreSQL.<br />
<br />
Get it from github: <a href="http://github.com/kimxilxyong/gorp" target="_blank"><span style="font-family: "Courier New", Courier, monospace;">http://github.com/kimxilxyong/gorp</span> </a><br />
<br />
The latest (as of 2015.05.29) upstream changes have been merged, the code is now ready for testers.<br />
<br />
If you want to use "Gorp with Indexes" just add index tags to your table struct<br />
(<span style="font-family: "Courier New",Courier,monospace;">idx_user</span> in this example) and add a import <span style="color: green;">"github.com/kimxilxyong/gorp"</span><br />
<br />
<span style="color: navy; font-weight: 600;">type</span><span style="color: silver;"> </span><span style="color: navy;">Post</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">struct</span><span style="color: silver;"> </span><span style="color: black;">{</span>
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Id<span style="color: silver;"> </span><span style="color: navy;">uint64</span><span style="color: silver;"> </span><span style="color: green;">`db:"notnull,</span><span style="color: silver;"> </span><span style="color: green;">PID,</span><span style="color: silver;"> </span><span style="color: green;">primarykey,</span><span style="color: silver;"> </span><span style="color: green;">autoincrement"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>User<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"index:idx_user,</span><span style="color: silver;"> </span><span style="color: green;">size:64"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>PostSub<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"index:idx_user,</span><span style="color: silver;"> </span><span style="color: green;">size:128"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>UserIP<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"notnull,</span><span style="color: silver;"> </span><span style="color: green;">index:idx_user,</span><span style="color: silver;"> </span><span style="color: green;">size:16"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>BodyType<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"notnull,</span><span style="color: silver;"> </span><span style="color: green;">size:64"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Body<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"name:PostBody,</span><span style="color: silver;"> </span><span style="color: green;">size:16384"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Err<span style="color: silver;"> </span><span style="color: navy;">error</span><span style="color: silver;"> </span><span style="color: green;">`db:"-"`</span><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">ignore</span><span style="color: silver;"> </span><span style="color: green;">this</span><span style="color: silver;"> </span><span style="color: green;">field</span><span style="color: silver;"> </span><span style="color: green;">when</span><span style="color: silver;"> </span><span style="color: green;">storing</span><span style="color: silver;"> </span><span style="color: green;">with</span><span style="color: silver;"> </span><span style="color: green;">gorp</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<br />
Then you only need to call: <span style="font-family: "Courier New",Courier,monospace;">CreateIndexes</span><br />
<br />
<span style="color: silver;"> </span>tablename<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: green;">"posts_index_test"</span>
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">SetKeys(true)</span><span style="color: silver;"> </span><span style="color: green;">means</span><span style="color: silver;"> </span><span style="color: green;">we</span><span style="color: silver;"> </span><span style="color: green;">have</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">auto</span><span style="color: silver;"> </span><span style="color: green;">increment</span><span style="color: silver;"> </span><span style="color: green;">primary</span><span style="color: silver;"> </span><span style="color: green;">key,</span><span style="color: silver;"> </span><span style="color: green;">which</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">will</span><span style="color: silver;"> </span><span style="color: green;">get</span><span style="color: silver;"> </span><span style="color: green;">automatically</span><span style="color: silver;"> </span><span style="color: green;">bound</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">your</span><span style="color: silver;"> </span><span style="color: green;">struct</span><span style="color: silver;"> </span><span style="color: green;">post-insert</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>table<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>dbmap<span style="color: black;">.</span>AddTableWithName<span style="color: black;">(</span>post<span style="color: black;">.</span>Post<span style="color: black;">{},</span><span style="color: silver;"> </span>tablename<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>table<span style="color: black;">.</span>SetKeys<span style="color: black;">(</span><span style="color: navy;">true</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"PID"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">create</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">table.</span><span style="color: silver;"> </span><span style="color: green;">in</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">production</span><span style="color: silver;"> </span><span style="color: green;">system</span><span style="color: silver;"> </span><span style="color: green;">you'd</span><span style="color: silver;"> </span><span style="color: green;">generally</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">use</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">migration</span><span style="color: silver;"> </span><span style="color: green;">tool,</span><span style="color: silver;"> </span><span style="color: green;">or</span><span style="color: silver;"> </span><span style="color: green;">create</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">tables</span><span style="color: silver;"> </span><span style="color: green;">via</span><span style="color: silver;"> </span><span style="color: green;">scripts</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>dbmap<span style="color: black;">.</span>CreateTablesIfNotExists<span style="color: black;">();</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>errors<span style="color: black;">.</span>New<span style="color: black;">(</span><span style="color: green;">"Create</span><span style="color: silver;"> </span><span style="color: green;">tables</span><span style="color: silver;"> </span><span style="color: green;">failed:</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>err<span style="color: black;">.</span>Error<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Force</span><span style="color: silver;"> </span><span style="color: green;">create</span><span style="color: silver;"> </span><span style="color: green;">all</span><span style="color: silver;"> </span><span style="color: green;">indexes</span><span style="color: silver;"> </span><span style="color: green;">for</span><span style="color: silver;"> </span><span style="color: green;">this</span><span style="color: silver;"> </span><span style="color: green;">database</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>dbmap<span style="color: black;">.</span>CreateIndexes<span style="color: black;">();</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>errors<span style="color: black;">.</span>New<span style="color: black;">(</span><span style="color: green;">"Create</span><span style="color: silver;"> </span><span style="color: green;">indexes</span><span style="color: silver;"> </span><span style="color: green;">failed:</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>err<span style="color: black;">.</span>Error<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com0tag:blogger.com,1999:blog-3324173739769750106.post-84036541050058403872015-05-21T12:44:00.000+00:002015-06-04T05:14:23.248+00:00Day 11 - Index creation and primary keys in gorp, gorp with indexesAs index creation is currently not available in the gorp master <a href="https://github.com/go-gorp/gorp" target="_blank">https://github.com/go-gorp/gorp</a> I did as suggested by github, created a fork and added the index creation code myself. I think thats how opensource should work: Create a fork with experimental features and if they are stable and tested it will get merged back into the origin (eventually).<br />
<br />
The test is to crawl content from Reddit and store it into a mysql database. The interresting part is in:<br />
<br />
App Logic: <a href="https://github.com/kimxilxyong/intogooglego/blob/master/redditFetchGorp/redditFetchGorp.go" target="_blank">Crawler</a><br />
Gorp Struct: <a href="https://github.com/kimxilxyong/intogooglego/blob/master/post/post.go" target="_blank">The Post struct which gets stored to mysql</a><br />
<br />
If you dont already know how to use <span style="font-family: "Courier New",Courier,monospace;">git</span> or <span style="font-family: "Courier New",Courier,monospace;">go get</span> please check this out: <a href="http://githowto.com/" target="_blank"> http://githowto.com/</a><br />
<br />
The key part is the Post struct, which defines how the Posts table in mysql will be created:<br />
<br />
Note: the table creation will NOT work with the master gorp, you have to use: <a href="https://github.com/kimxilxyong/gorp" target="_blank">gorp with indexes</a><br />
<br />
<br />
<span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">holds</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">single</span><span style="color: silver;"> </span><span style="color: green;">post</span>
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">type</span><span style="color: silver;"> </span><span style="color: navy;">Post</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">struct</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Id<span style="color: silver;"> </span><span style="color: navy;">uint64</span><span style="color: silver;"> </span><span style="color: green;">`db:"notnull,</span><span style="color: silver;"> </span><span style="color: green;">PID,</span><span style="color: silver;"> </span><span style="color: green;">primarykey,</span><span style="color: silver;"> </span><span style="color: green;">autoincrement"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>SecondTestID<span style="color: silver;"> </span><span style="color: navy;">int</span><span style="color: silver;"> </span><span style="color: green;">`db:"notnull,</span><span style="color: silver;"> </span><span style="color: green;">name:</span><span style="color: silver;"> </span><span style="color: green;">SID"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Created<span style="color: silver;"> </span>time<span style="color: black;">.</span>Time<span style="color: silver;"> </span><span style="color: green;">`db:"notnull,</span><span style="color: silver;"> </span><span style="color: green;">primarykey"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>PostDate<span style="color: silver;"> </span>time<span style="color: black;">.</span>Time<span style="color: silver;"> </span><span style="color: green;">`db:"notnull"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Site<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"name:</span><span style="color: silver;"> </span><span style="color: green;">PostSite,</span><span style="color: silver;"> </span><span style="color: green;">notnull,</span><span style="color: silver;"> </span><span style="color: green;">size:50"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>PostId<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"notnull,</span><span style="color: silver;"> </span><span style="color: green;">size:32,</span><span style="color: silver;"> </span><span style="color: green;">unique"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Score<span style="color: silver;"> </span><span style="color: navy;">int</span><span style="color: silver;"> </span><span style="color: green;">`db:"notnull"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Title<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"notnull"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Url<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"notnull"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>User<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"index:idx_user,</span><span style="color: silver;"> </span><span style="color: green;">size:64"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>PostSub<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"index:idx_user,</span><span style="color: silver;"> </span><span style="color: green;">size:128"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>UserIP<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"notnull,</span><span style="color: silver;"> </span><span style="color: green;">size:16"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>BodyType<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"notnull,</span><span style="color: silver;"> </span><span style="color: green;">size:64"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Body<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`db:"name:PostBody,</span><span style="color: silver;"> </span><span style="color: green;">size:16384"`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Err<span style="color: silver;"> </span><span style="color: navy;">error</span><span style="color: silver;"> </span><span style="color: green;">`db:"-"`</span><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">ignore</span><span style="color: silver;"> </span><span style="color: green;">this</span><span style="color: silver;"> </span><span style="color: green;">field</span><span style="color: silver;"> </span><span style="color: green;">when</span><span style="color: silver;"> </span><span style="color: green;">storing</span><span style="color: silver;"> </span><span style="color: green;">with</span><span style="color: silver;"> </span><span style="color: green;">gorp</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
<br />
The part of the source which connects your go code to the database is <a href="https://github.com/kimxilxyong/intogooglego/blob/master/redditFetchGorp/redditFetchGorp.go" target="_blank">redditFetchGorp</a>:<br />
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">connect</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">db</span><span style="color: silver;"> </span><span style="color: green;">using</span><span style="color: silver;"> </span><span style="color: green;">standard</span><span style="color: silver;"> </span><span style="color: green;">Go</span><span style="color: silver;"> </span><span style="color: green;">database/sql</span><span style="color: silver;"> </span><span style="color: green;">API</span>
<span style="color: silver;"> </span><span style="color: green;">//db,</span><span style="color: silver;"> </span><span style="color: green;">err</span><span style="color: silver;"> </span><span style="color: green;">:=</span><span style="color: silver;"> </span><span style="color: green;">sql.Open("mysql",</span><span style="color: silver;"> </span><span style="color: green;">"user:password@/dbname")</span>
<span style="color: silver;"> </span>db<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>sql<span style="color: black;">.</span>Open<span style="color: black;">(</span><span style="color: green;">"mysql"</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"golang:golang@/golang"</span><span style="color: black;">)</span>
<span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span>
<span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>errors<span style="color: black;">.</span>New<span style="color: black;">(</span><span style="color: green;">"sql.Open</span><span style="color: silver;"> </span><span style="color: green;">failed:</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>err<span style="color: black;">.</span>Error<span style="color: black;">())</span>
<span style="color: silver;"> </span><span style="color: black;">}</span>
<span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">construct</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">gorp</span><span style="color: silver;"> </span><span style="color: green;">DbMap</span>
<span style="color: silver;"> </span>dbmap<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: black;">&</span>gorp<span style="color: black;">.</span>DbMap<span style="color: black;">{</span>Db<span style="color: black;">:</span><span style="color: silver;"> </span>db<span style="color: black;">,</span><span style="color: silver;"> </span>Dialect<span style="color: black;">:</span><span style="color: silver;"> </span>gorp<span style="color: black;">.</span>MySQLDialect<span style="color: black;">{</span><span style="color: green;">"InnoDB"</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"UTF8"</span><span style="color: black;">}}</span>
<span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">defer</span><span style="color: silver;"> </span>dbmap<span style="color: black;">.</span>Db<span style="color: black;">.</span>Close<span style="color: black;">()</span>
<span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">register</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">structs</span><span style="color: silver;"> </span><span style="color: green;">you</span><span style="color: silver;"> </span><span style="color: green;">wish</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">use</span><span style="color: silver;"> </span><span style="color: green;">with</span><span style="color: silver;"> </span><span style="color: green;">gorp</span>
<span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">you</span><span style="color: silver;"> </span><span style="color: green;">can</span><span style="color: silver;"> </span><span style="color: green;">also</span><span style="color: silver;"> </span><span style="color: green;">use</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">shorter</span><span style="color: silver;"> </span><span style="color: green;">dbmap.AddTable()</span><span style="color: silver;"> </span><span style="color: green;">if</span><span style="color: silver;"> </span><span style="color: green;">you</span>
<span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">don't</span><span style="color: silver;"> </span><span style="color: green;">want</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">override</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">table</span><span style="color: silver;"> </span><span style="color: green;">name</span>
<span style="color: silver;"> </span>_<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>dbmap<span style="color: black;">.</span>AddTableWithName<span style="color: black;">(</span>post<span style="color: black;">.</span>Post<span style="color: black;">{},</span><span style="color: silver;"> </span><span style="color: green;">"posts"</span><span style="color: black;">)</span>
<span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">create</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">table.</span><span style="color: silver;"> </span><span style="color: green;">in</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">production</span><span style="color: silver;"> </span><span style="color: green;">system</span><span style="color: silver;"> </span><span style="color: green;">you'd</span><span style="color: silver;"> </span><span style="color: green;">generally</span>
<span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">use</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">migration</span><span style="color: silver;"> </span><span style="color: green;">tool,</span><span style="color: silver;"> </span><span style="color: green;">or</span><span style="color: silver;"> </span><span style="color: green;">create</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">tables</span><span style="color: silver;"> </span><span style="color: green;">via</span><span style="color: silver;"> </span><span style="color: green;">scripts</span>
<span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>dbmap<span style="color: black;">.</span>CreateTablesIfNotExists<span style="color: black;">()</span>
<span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span>
<span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>errors<span style="color: black;">.</span>New<span style="color: black;">(</span><span style="color: green;">"Create</span><span style="color: silver;"> </span><span style="color: green;">table</span><span style="color: silver;"> </span><span style="color: green;">'posts'</span><span style="color: silver;"> </span><span style="color: green;">failed:</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>err<span style="color: black;">.</span>Error<span style="color: black;">())</span>
<span style="color: silver;"> </span><span style="color: black;">}</span> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
<br />
The resulting table should be:<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">CREATE TABLE `posts` (
`PID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`SID` int(11) NOT NULL,
`Created` datetime NOT NULL,
`PostDate` datetime NOT NULL,
`PostSite` varchar(50) NOT NULL,
`PostId` varchar(32) NOT NULL,
`Score` int(11) NOT NULL,
`Title` varchar(255) NOT NULL,
`Url` varchar(255) NOT NULL,
`User` varchar(64) DEFAULT NULL,
`PostSub` varchar(128) DEFAULT NULL,
`UserIP` varchar(16) NOT NULL,
`BodyType` varchar(64) NOT NULL,
`PostBody` varchar(16384) DEFAULT NULL,
PRIMARY KEY (`PID`,`Created`),
UNIQUE KEY `PostId` (`PostId`),
KEY `idx_user` (`User`,`PostSub`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;</span></pre>
Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com0tag:blogger.com,1999:blog-3324173739769750106.post-54785238603652420722015-05-18T10:33:00.001+00:002015-05-18T10:33:17.699+00:00Day 10 - ORM comparison in Go using MySql, the rest of the packThe following "ORM's" i did not actually test, just googled it. Maybe the info is usable for somebody.<br />
StarDate is 2015.05.18<br />
<br />
<b>beedb</b> :although beedb has been called <a href="http://astaxie.gitbooks.io/build-web-application-with-golang/content/en/05.5.html" target="_blank">dead</a> and unmaintained, it seems to be still alive as a sub of <a href="https://github.com/astaxie/beego" target="_blank">beego</a>: <a href="https://github.com/astaxie/beego/tree/master/orm" target="_blank">beego orm</a>. But as it is part of a larger framework I have pushed it back in my queue.<br />
<br />
<b>qbs</b>: This looks to me like an awesome lib and it is for sure worth further examining: It can create Indexes and does so even on joined/embedded structs. Note to myself: Do a separate post on <a href="https://github.com/coocood/qbs" target="_blank">qbs!</a><br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com3tag:blogger.com,1999:blog-3324173739769750106.post-48587268441397145342015-05-15T08:02:00.000+00:002015-05-15T15:27:33.467+00:00Day 9 - ORM comparison in Go using MySql, today gormToday I tested <a href="https://github.com/jinzhu/gorm" target="_blank">gorm</a>, which looked very promising at the first glance: You can create indexes from field tags which is not possible in <a href="https://github.com/go-gorp/gorp" target="_blank">gorp</a> currently.<br />
<br />
<style type="text/css">
p, li { white-space: pre-wrap; }
</style>
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">type</span><span style="color: silver;"> </span><span style="color: navy;">TestDatabaseTableStruct</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">struct</span><span style="color: silver;"> </span><span style="color: black;">{</span><span style="color: silver;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>ID<span style="color: silver;"> </span><span style="color: navy;">int</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Name<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`sql:"index:idx_name_code"`</span><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Create</span><span style="color: silver;"> </span><span style="color: green;">index</span><span style="color: silver;"> </span><span style="color: green;">with</span><span style="color: silver;"> </span><span style="color: green;">name,</span><span style="color: silver;"> </span><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">will</span><span style="color: silver;"> </span><span style="color: green;">create</span><span style="color: silver;"> </span><span style="color: green;">combined</span><span style="color: silver;"> </span><span style="color: green;">index</span><span style="color: silver;"> </span><span style="color: green;">if</span><span style="color: silver;"> </span><span style="color: green;">find</span><span style="color: silver;"> </span><span style="color: green;">other</span><span style="color: silver;"> </span><span style="color: green;">fields</span><span style="color: silver;"> </span><span style="color: green;">defined</span><span style="color: silver;"> </span><span style="color: green;">same</span><span style="color: silver;"> </span><span style="color: green;">name</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Code<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: green;">`sql:"index:idx_name_code"`</span><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">`unique_index`</span><span style="color: silver;"> </span><span style="color: green;">also</span><span style="color: silver;"> </span><span style="color: green;">works</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<br />
But then I started to implement my simple "insert a post into one table" example where the pain started:<br />
<br />
The interface style of gorm feels very strange, even for me, coming from a C/C++ and Delphi background. This style is all over the place, calling a method on a DB pointer and getting back a DB pointer ?!?<br />
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">(</span>s<span style="color: silver;"> </span><span style="color: black;">*</span><span style="color: navy;">DB</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">Where(</span>query<span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">interface</span><span style="color: black;">{},</span><span style="color: silver;"> </span>args<span style="color: silver;"> </span><span style="color: black;">...</span><span style="color: navy; font-weight: 600;">interface</span><span style="color: black;">{})</span><span style="color: silver;"> </span><span style="color: black;">*</span>DB<span style="color: silver;"> </span><span style="color: black;">{</span>
<span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>s<span style="color: black;">.</span>clone<span style="color: black;">().</span>search<span style="color: black;">.</span>Where<span style="color: black;">(</span>query<span style="color: black;">,</span><span style="color: silver;"> </span>args<span style="color: black;">...).</span>db
<span style="color: black;">}</span> </span></pre>
<br />
I dont get it, maybe some people smarter than me can understand this.<br />
I assume this strange interface of gorm is because of the "chainability" of it.<br />
<br />
But i dont need chainability, because I am able to write SQL!<br />
<br />
I struggled for 5 hours to make a simple "insert or update if exists" logic to get to work. I have given up! Gorm does so much annoying magic, like changing table and field names, its just too much hassle to learn another ORM layer when you would be better off learning SQL. <br />
<br />
Im not even posting snippets, because I am so annoyed<a href="http://www.dict.cc/englisch-deutsch/to+be+annoyed.html"></a>, the incomplete test source for gorm ist at <a href="https://github.com/kimxilxyong/intogooglego/tree/master/redditFetchGorm" target="_blank">gorm example</a> .<br />
<br />
My conclusion is: If you cant or dont want to write SQL you "could" use gorm, for everybody else use <a href="https://github.com/go-gorp/gorp" target="_blank">gorp</a>.<br />
<br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com2tag:blogger.com,1999:blog-3324173739769750106.post-45402902616406704042015-05-14T17:17:00.001+00:002015-05-22T08:04:04.053+00:00Day 8 - ORM comparison in Go using MySql, today gorpThe first ORM im testing is <a href="https://github.com/go-gorp/gorp" target="_blank">gorp</a>, because its the first that poked my eye, is easy to use and is not part of a larger framework.<br />
<br />
As my code is growing im not posting the <a href="https://github.com/kimxilxyong/intogooglego/tree/master/redditFetchGorp" target="_blank">full source</a>, but only snippets which handle gorp code.<br />
<br />
To test it you need a MySql installation, create a Schema golang for a user golang with password golang: <a href="http://webvaultwiki.com.au/%28S%28iungfwenvz2f5c55pioeinil%29%29/Default.aspx?Page=Create-Mysql-Database-User-Workbench&NS=&AspxAutoDetectCookieSupport=1" target="_blank">MySql Readme</a> <br />
<br />
Open the database:<br />
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>db<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>sql<span style="color: black;">.</span>Open<span style="color: black;">(</span><span style="color: green;">"mysql"</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"golang:golang@/golang"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>errors<span style="color: black;">.</span>New<span style="color: black;">(</span><span style="color: green;">"sql.Open</span><span style="color: silver;"> </span><span style="color: green;">failed:</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>err<span style="color: black;">.</span>Error<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Open</span><span style="color: silver;"> </span><span style="color: green;">doesn't</span><span style="color: silver;"> </span><span style="color: green;">open</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">connection.</span><span style="color: silver;"> </span><span style="color: green;">Validate</span><span style="color: silver;"> </span><span style="color: green;">DSN</span><span style="color: silver;"> </span><span style="color: green;">data:</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>db<span style="color: black;">.</span>Ping<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>errors<span style="color: black;">.</span>New<span style="color: black;">(</span><span style="color: green;">"db.Ping</span><span style="color: silver;"> </span><span style="color: green;">failed:</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>err<span style="color: black;">.</span>Error<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">construct</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">gorp</span><span style="color: silver;"> </span><span style="color: green;">DbMap</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>dbmap<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: black;">&</span>gorp<span style="color: black;">.</span>DbMap<span style="color: black;">{</span>Db<span style="color: black;">:</span><span style="color: silver;"> </span>db<span style="color: black;">,</span><span style="color: silver;"> </span>Dialect<span style="color: black;">:</span><span style="color: silver;"> </span>gorp<span style="color: black;">.</span>MySQLDialect<span style="color: black;">{</span><span style="color: green;">"InnoDB"</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"UTF8"</span><span style="color: black;">}}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">defer</span><span style="color: silver;"> </span>dbmap<span style="color: black;">.</span>Db<span style="color: black;">.</span>Close<span style="color: black;">()</span></pre>
<br />
<br />
Create a table if it does not exist:<br />
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">// </span><span style="color: green;"><span style="color: silver;"></span><span style="color: green;">SetKeys(true)</span><span style="color: silver;"> </span><span style="color: green;">means</span><span style="color: silver;"> </span><span style="color: green;">we</span><span style="color: silver;"> </span><span style="color: green;">have</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">auto</span><span style="color: silver;"> </span><span style="color: green;">increment</span><span style="color: silver;"> </span><span style="color: green;">primary</span><span style="color: silver;"> </span><span style="color: green;">key,</span><span style="color: silver;"> </span><span style="color: green;">which</span> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;"> //</span><span style="color: silver;"> </span><span style="color: green;">will</span><span style="color: silver;"> </span><span style="color: green;">get</span><span style="color: silver;"> </span><span style="color: green;">automatically</span><span style="color: silver;"> </span><span style="color: green;">bound</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">your</span><span style="color: silver;"> </span><span style="color: green;">struct</span><span style="color: silver;"> </span><span style="color: green;">post-insert</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>table<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>dbmap<span style="color: black;">.</span>AddTableWithName<span style="color: black;">(</span>post<span style="color: black;">.</span>Post<span style="color: black;">{},</span><span style="color: silver;"> </span><span style="color: green;">"posts"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>table<span style="color: black;">.</span>SetKeys<span style="color: black;">(</span><span style="color: navy;">true</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"Id"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
Check if the post already is in the database:<br />
<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> // </span><span style="color: green;">check</span><span style="color: silver;"> </span><span style="color: green;">if</span><span style="color: silver;"> </span><span style="color: green;">post</span><span style="color: silver;"> </span><span style="color: green;">already</span><span style="color: silver;"> </span><span style="color: green;">exists</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>count<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>dbmap<span style="color: black;">.</span>SelectInt<span style="color: black;">(</span><span style="color: green;">"select</span><span style="color: silver;"> </span><span style="color: green;">count(*)</span><span style="color: silver;"> </span><span style="color: green;">from</span><span style="color: silver;"> </span><span style="color: green;">posts</span><span style="color: silver;"> </span><span style="color: green;">where</span><span style="color: silver;"> </span><span style="color: green;">PostId</span><span style="color: silver;"> </span><span style="color: green;">=</span><span style="color: silver;"> </span><span style="color: green;">?"</span><span style="color: black;">,</span><span style="color: silver;"> </span>post<span style="color: black;">.</span>PostId<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>errors<span style="color: black;">.</span>New<span style="color: black;">(</span><span style="color: green;">"select</span><span style="color: silver;"> </span><span style="color: green;">count(*)</span><span style="color: silver;"> </span><span style="color: green;">from</span><span style="color: silver;"> </span><span style="color: green;">posts</span><span style="color: silver;"> </span><span style="color: green;">failed:</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>err<span style="color: black;">.</span>Error<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<br />
Do an insert if count == 0<br />
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;"> err<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>dbmap<span style="color: black;">.</span>Insert<span style="color: black;">(&</span>post<span style="color: black;">)</span> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;"> if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>errors<span style="color: black;">.</span>New<span style="color: black;">(</span><span style="color: green;">"insert</span><span style="color: silver;"> </span><span style="color: green;">into</span><span style="color: silver;"> </span><span style="color: green;">table</span><span style="color: silver;"> </span><span style="color: green;">posts</span><span style="color: silver;"> </span><span style="color: green;">failed:</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>err<span style="color: black;">.</span>Error<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<br />
.<br />
Edit 2015.05.21:<br />
The code for fetching and the post structure have been changed to use <a href="https://github.com/kimxilxyong/gorp" target="_blank">gorp with indexes</a>, please look at <a href="http://intogooglego.blogspot.co.at/2015/05/day-11-index-creation-and-primary-keys.html" target="_blank">Day 11</a> of my blog for details how to create indexes with gorp.<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com0tag:blogger.com,1999:blog-3324173739769750106.post-91136755485127492692015-05-08T09:56:00.001+00:002015-05-08T10:05:38.119+00:00Day 7 - GoQuery HTML parsingThis post is about testing <a href="https://github.com/PuerkitoBio/goquery" target="_blank">GoQuery</a> from <a href="https://twitter.com/PuerkitoBio" target="_blank">PuerkitoBio</a> (tnx for this amazing lib!), a library for parsing HTML, which I intend to use in my webcrawler.<br />
Its syntax is similar to jQuery ( <a href="https://github.com/PuerkitoBio/goquery/blob/master/README.md" target="_blank">Readme</a> ), so i thought thats cool, lots of people use jQuery and its well documented. Furthermore its much less boilerplate code than the plain <a href="https://godoc.org/golang.org/x/net/html" target="_blank">net/html</a> package which comes with Go.<br />
<br />
I had some issues to wrap my head around to understand that "<span style="color: green;">.title</span><span style="color: silver;"></span><span style="color: green;">.may-blank</span>"<span style="color: green;"> <span style="color: black;">is different </span></span><br />
<span style="color: green;"><span style="color: black;">from </span></span><span style="color: green;"><span style="color: black;">"<span style="color: green;">.title</span><span style="color: silver;"> </span><span style="color: green;">.may-blank</span>"<span style="color: green;"></span> (note the space). The first searches for an element with </span></span><br />
<span style="color: green;"><span style="color: black;">class="title may-blank", and the second one searches for a class="title" with a child class="may-blank".</span></span><br />
<br />
<span style="color: green;"><span style="color: black;">After scratching my head for several hours i finally decided to look into the jQuery documentation - aaaand Bingo!! (To note is that I hate JavaScript since the 90s and try to avoid it whenever possible) </span></span><br />
<span style="color: green;"><span style="color: black;"><br /></span></span>
<span style="color: green;"><span style="color: black;">Here is my test code, parsing posts </span></span><span style="color: green;"><span style="color: black;"><span style="color: green;"><span style="color: black;">(from a static html) </span></span>from reddit:</span></span><br />
<span style="color: green;"><span style="color: black;">The test HTML was copied from within the firefox inspector and then formatted by the Tidy2 plugin for Notebook++</span></span><br />
<br />
<span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;"><span style="color: navy; font-weight: 600;">package</span><span style="color: silver;"> </span>main
</span></span><br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">import</span><span style="color: silver;"> </span><span style="color: black;">(</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"fmt"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"github.com/PuerkitoBio/goquery"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"io"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"log"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"strings"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"unicode"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">ExampleGoQuery()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Get</span><span style="color: silver;"> </span><span style="color: green;">an</span><span style="color: silver;"> </span><span style="color: green;">io.Reader</span><span style="color: silver;"> </span><span style="color: green;">with</span><span style="color: silver;"> </span><span style="color: green;">HTML</span><span style="color: silver;"> </span><span style="color: green;">content</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>io<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>getHtmlInputReader<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Create</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">qoquery</span><span style="color: silver;"> </span><span style="color: green;">document</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">parse</span><span style="color: silver;"> </span><span style="color: green;">from</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>doc<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>goquery<span style="color: black;">.</span>NewDocumentFromReader<span style="color: black;">(</span>io<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>checkErr<span style="color: black;">(</span>err<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"Failed</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">parse</span><span style="color: silver;"> </span><span style="color: green;">HTML"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Println<span style="color: black;">(</span><span style="color: green;">"----</span><span style="color: silver;"> </span><span style="color: green;">Starting</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">parse</span><span style="color: silver;"> </span><span style="color: green;">------------------------"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Find</span><span style="color: silver;"> </span><span style="color: green;">reddit</span><span style="color: silver;"> </span><span style="color: green;">posts</span><span style="color: silver;"> </span><span style="color: green;">=</span><span style="color: silver;"> </span><span style="color: green;">elements</span><span style="color: silver;"> </span><span style="color: green;">with</span><span style="color: silver;"> </span><span style="color: green;">class</span><span style="color: silver;"> </span><span style="color: green;">"thing"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>thing<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>doc<span style="color: black;">.</span>Find<span style="color: black;">(</span><span style="color: green;">".thing"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">for</span><span style="color: silver;"> </span>iThing<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">range</span><span style="color: silver;"> </span>thing<span style="color: black;">.</span>Nodes<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">use</span><span style="color: silver;"> </span><span style="color: green;">`single`</span><span style="color: silver;"> </span><span style="color: green;">as</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">selection</span><span style="color: silver;"> </span><span style="color: green;">of</span><span style="color: silver;"> </span><span style="color: green;">1</span><span style="color: silver;"> </span><span style="color: green;">node</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>singlething<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>thing<span style="color: black;">.</span>Eq<span style="color: black;">(</span>iThing<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">get</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">reddit</span><span style="color: silver;"> </span><span style="color: green;">post</span><span style="color: silver;"> </span><span style="color: green;">identifier</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>reddit_post_id<span style="color: black;">,</span><span style="color: silver;"> </span>exists<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>singlething<span style="color: black;">.</span>Attr<span style="color: black;">(</span><span style="color: green;">"data-fullname"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>exists<span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span><span style="color: navy;">true</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">find</span><span style="color: silver;"> </span><span style="color: green;">an</span><span style="color: silver;"> </span><span style="color: green;">element</span><span style="color: silver;"> </span><span style="color: green;">with</span><span style="color: silver;"> </span><span style="color: green;">class</span><span style="color: silver;"> </span><span style="color: green;">title</span><span style="color: silver;"> </span><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">child</span><span style="color: silver;"> </span><span style="color: green;">with</span><span style="color: silver;"> </span><span style="color: green;">class</span><span style="color: silver;"> </span><span style="color: green;">may-blank</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>reddit_post_title<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>singlething<span style="color: black;">.</span>Find<span style="color: black;">(</span><span style="color: green;">".title</span><span style="color: silver;"> </span><span style="color: green;">.may-blank"</span><span style="color: black;">).</span>Text<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>reddit_post_user<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>singlething<span style="color: black;">.</span>Find<span style="color: black;">(</span><span style="color: green;">".author"</span><span style="color: black;">).</span>Text<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">find</span><span style="color: silver;"> </span><span style="color: green;">an</span><span style="color: silver;"> </span><span style="color: green;">element</span><span style="color: silver;"> </span><span style="color: green;">with</span><span style="color: silver;"> </span><span style="color: green;">class</span><span style="color: silver;"> </span><span style="color: green;">comments</span><span style="color: silver;"> </span><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">may-blank</span><span style="color: silver;"> </span><span style="color: green;">(in</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">same</span><span style="color: silver;"> </span><span style="color: green;">element,</span><span style="color: silver;"> </span><span style="color: green;">note</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">space!)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>reddit_post_url<span style="color: black;">,</span><span style="color: silver;"> </span>_<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>singlething<span style="color: black;">.</span>Find<span style="color: black;">(</span><span style="color: green;">".comments.may-blank"</span><span style="color: black;">).</span>Attr<span style="color: black;">(</span><span style="color: green;">"href"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>reddit_post_score<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>singlething<span style="color: black;">.</span>Find<span style="color: black;">(</span><span style="color: green;">".score.likes"</span><span style="color: black;">).</span>Text<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>reddit_postdate<span style="color: black;">,</span><span style="color: silver;"> </span>exists<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>singlething<span style="color: black;">.</span>Find<span style="color: black;">(</span><span style="color: green;">"time"</span><span style="color: black;">).</span>Attr<span style="color: black;">(</span><span style="color: green;">"datetime"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>exists<span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span><span style="color: navy;">true</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Remove</span><span style="color: silver;"> </span><span style="color: green;">CRLF</span><span style="color: silver;"> </span><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">unnecessary</span><span style="color: silver;"> </span><span style="color: green;">whitespaces</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>reddit_post_title<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>stringMinifier<span style="color: black;">(</span>reddit_post_title<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Print</span><span style="color: silver;"> </span><span style="color: green;">out</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">crawled</span><span style="color: silver;"> </span><span style="color: green;">info</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Println<span style="color: black;">(</span><span style="color: green;">"Id</span><span style="color: silver;"> </span><span style="color: green;">=</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>reddit_post_id<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Println<span style="color: black;">(</span><span style="color: green;">"Date</span><span style="color: silver;"> </span><span style="color: green;">=</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>reddit_postdate<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Println<span style="color: black;">(</span><span style="color: green;">"User</span><span style="color: silver;"> </span><span style="color: green;">=</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>reddit_post_user<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Println<span style="color: black;">(</span><span style="color: green;">"Title</span><span style="color: silver;"> </span><span style="color: green;">=</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>reddit_post_title<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Println<span style="color: black;">(</span><span style="color: green;">"Score</span><span style="color: silver;"> </span><span style="color: green;">=</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>reddit_post_score<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Println<span style="color: black;">(</span><span style="color: green;">"Url</span><span style="color: silver;"> </span><span style="color: green;">=</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>reddit_post_url<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Println<span style="color: black;">(</span><span style="color: green;">"-----------------------------------------------"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Removes</span><span style="color: silver;"> </span><span style="color: green;">all</span><span style="color: silver;"> </span><span style="color: green;">unnecessary</span><span style="color: silver;"> </span><span style="color: green;">whitespaces</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">stringMinifier(</span>in<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">(</span>out<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>white<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy;">false</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">for</span><span style="color: silver;"> </span>_<span style="color: black;">,</span><span style="color: silver;"> </span>c<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">range</span><span style="color: silver;"> </span>in<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>unicode<span style="color: black;">.</span>IsSpace<span style="color: black;">(</span>c<span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span><span style="color: black;">!</span>white<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>out<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>out<span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: green;">"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>white<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span><span style="color: navy;">true</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">else</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>out<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>out<span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: black;">(</span>c<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>white<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span><span style="color: navy;">false</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">returns</span><span style="color: silver;"> </span><span style="color: green;">an</span><span style="color: silver;"> </span><span style="color: green;">io.Reader</span><span style="color: silver;"> </span><span style="color: green;">with</span><span style="color: silver;"> </span><span style="color: green;">dummy</span><span style="color: silver;"> </span><span style="color: green;">test</span><span style="color: silver;"> </span><span style="color: green;">html</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">getHtmlInputReader()</span><span style="color: silver;"> </span>io<span style="color: black;">.</span>Reader<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>s<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: green;">`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;"><html></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><head></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><meta</span><span style="color: silver;"> </span><span style="color: green;">name="generator"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">content="HTML</span><span style="color: silver;"> </span><span style="color: green;">Tidy</span><span style="color: silver;"> </span><span style="color: green;">for</span><span style="color: silver;"> </span><span style="color: green;">HTML5</span><span style="color: silver;"> </span><span style="color: green;">(experimental)</span><span style="color: silver;"> </span><span style="color: green;">for</span><span style="color: silver;"> </span><span style="color: green;">Windows</span><span style="color: silver;"> </span><span style="color: green;">https://github.com/w3c/tidy-html5/tree/c63cc39"</span><span style="color: silver;"> </span><span style="color: green;">/></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><title></title></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></head></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><body></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="thing</span><span style="color: silver;"> </span><span style="color: green;">id-t3_34z9xo</span><span style="color: silver;"> </span><span style="color: green;">odd</span><span style="color: silver;"> </span><span style="color: green;">link"</span><span style="color: silver;"> </span><span style="color: green;">onclick="click_thing(this)"</span><span style="color: silver;"> </span><span style="color: green;">data-fullname="t3_34z9xo"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><p</span><span style="color: silver;"> </span><span style="color: green;">class="parent"></p></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><span</span><span style="color: silver;"> </span><span style="color: green;">class="rank">1</span></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="midcol</span><span style="color: silver;"> </span><span style="color: green;">unvoted"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="arrow</span><span style="color: silver;"> </span><span style="color: green;">up</span><span style="color: silver;"> </span><span style="color: green;">login-required"</span><span style="color: silver;"> </span><span style="color: green;">onclick="$(this).vote(r.config.vote_hash,</span><span style="color: silver;"> </span><span style="color: green;">null,</span><span style="color: silver;"> </span><span style="color: green;">event)"</span><span style="color: silver;"> </span><span style="color: green;">role="button"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">aria-label="upvote"</span><span style="color: silver;"> </span><span style="color: green;">tabindex="0"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="score</span><span style="color: silver;"> </span><span style="color: green;">dislikes">10</div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="score</span><span style="color: silver;"> </span><span style="color: green;">unvoted">11</div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="score</span><span style="color: silver;"> </span><span style="color: green;">likes">12</div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="arrow</span><span style="color: silver;"> </span><span style="color: green;">down</span><span style="color: silver;"> </span><span style="color: green;">login-required"</span><span style="color: silver;"> </span><span style="color: green;">onclick="$(this).vote(r.config.vote_hash,</span><span style="color: silver;"> </span><span style="color: green;">null,</span><span style="color: silver;"> </span><span style="color: green;">event)"</span><span style="color: silver;"> </span><span style="color: green;">role="button"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">aria-label="downvote"</span><span style="color: silver;"> </span><span style="color: green;">tabindex="0"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="entry</span><span style="color: silver;"> </span><span style="color: green;">unvoted"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><p</span><span style="color: silver;"> </span><span style="color: green;">class="title"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">class="title</span><span style="color: silver;"> </span><span style="color: green;">may-blank</span><span style="color: silver;"> </span><span style="color: green;">loggedin"</span><span style="color: silver;"> </span><span style="color: green;">href="https://github.com/dariubs/GoBooks"</span><span style="color: silver;"> </span><span style="color: green;">tabindex="1">dariubs/GoBooks:</span><span style="color: silver;"> </span><span style="color: green;">list</span><span style="color: silver;"> </span><span style="color: green;">of</span><span style="color: silver;"> </span><span style="color: green;">paper</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">electronic</span><span style="color: silver;"> </span><span style="color: green;">books</span><span style="color: silver;"> </span><span style="color: green;">on</span><span style="color: silver;"> </span><span style="color: green;">Go</a></span><span style="color: silver;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><span</span><span style="color: silver;"> </span><span style="color: green;">class="domain">(</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">href="/domain/github.com/">github.com</a>)</span></p></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><p</span><span style="color: silver;"> </span><span style="color: green;">class="tagline">submitted</span><span style="color: silver;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><time</span><span style="color: silver;"> </span><span style="color: green;">title="Tue</span><span style="color: silver;"> </span><span style="color: green;">May</span><span style="color: silver;"> </span><span style="color: green;">5</span><span style="color: silver;"> </span><span style="color: green;">20:13:08</span><span style="color: silver;"> </span><span style="color: green;">2015</span><span style="color: silver;"> </span><span style="color: green;">UTC"</span><span style="color: silver;"> </span><span style="color: green;">datetime="2015-05-05T20:13:08+00:00"</span><span style="color: silver;"> </span><span style="color: green;">class="live-timestamp">10</span><span style="color: silver;"> </span><span style="color: green;">hours</span><span style="color: silver;"> </span><span style="color: green;">ago</time></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">href="http://www.reddit.com/user/dgryski"</span><span style="color: silver;"> </span><span style="color: green;">class="author</span><span style="color: silver;"> </span><span style="color: green;">may-blank</span><span style="color: silver;"> </span><span style="color: green;">id-t2_3hcmx">dgryski</a></p></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><ul</span><span style="color: silver;"> </span><span style="color: green;">class="flat-list</span><span style="color: silver;"> </span><span style="color: green;">buttons"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><li</span><span style="color: silver;"> </span><span style="color: green;">class="first"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">href="http://www.reddit.com/r/golang/comments/34z9xo/dariubsgobooks_list_of_paper_and_electronic_books/"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">class="comments</span><span style="color: silver;"> </span><span style="color: green;">may-blank">1</span><span style="color: silver;"> </span><span style="color: green;">comment</a></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></li></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><li</span><span style="color: silver;"> </span><span style="color: green;">class="share"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><span</span><span style="color: silver;"> </span><span style="color: green;">class="share-button</span><span style="color: silver;"> </span><span style="color: green;">toggle"</span><span style="color: silver;"> </span><span style="color: green;">style=""></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">class="option</span><span style="color: silver;"> </span><span style="color: green;">active</span><span style="color: silver;"> </span><span style="color: green;">login-required"</span><span style="color: silver;"> </span><span style="color: green;">href="#"</span><span style="color: silver;"> </span><span style="color: green;">tabindex="100"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">onclick="return</span><span style="color: silver;"> </span><span style="color: green;">toggle(this,</span><span style="color: silver;"> </span><span style="color: green;">share,</span><span style="color: silver;"> </span><span style="color: green;">cancelShare)">share</a></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">class="option"</span><span style="color: silver;"> </span><span style="color: green;">href="#">cancel</a></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></span></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></li></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><li</span><span style="color: silver;"> </span><span style="color: green;">class="link-save-button</span><span style="color: silver;"> </span><span style="color: green;">save-button"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">href="#">save</a></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></li></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><li></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><form</span><span style="color: silver;"> </span><span style="color: green;">action="/post/hide"</span><span style="color: silver;"> </span><span style="color: green;">method="post"</span><span style="color: silver;"> </span><span style="color: green;">class="state-button</span><span style="color: silver;"> </span><span style="color: green;">hide-button"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><input</span><span style="color: silver;"> </span><span style="color: green;">name="executed"</span><span style="color: silver;"> </span><span style="color: green;">value="hidden"</span><span style="color: silver;"> </span><span style="color: green;">type="hidden"</span><span style="color: silver;"> </span><span style="color: green;">/></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><span></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">href="javascript:void(0)"</span><span style="color: silver;"> </span><span style="color: green;">onclick="change_state(this,</span><span style="color: silver;"> </span><span style="color: green;">&#39;hide&#39;,</span><span style="color: silver;"> </span><span style="color: green;">hide_thing);">hide</a></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></span></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></form></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></li></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><li</span><span style="color: silver;"> </span><span style="color: green;">class="report-button"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">href="javascript:void(0)"</span><span style="color: silver;"> </span><span style="color: green;">class="action-thing"</span><span style="color: silver;"> </span><span style="color: green;">data-action-form="#report-action-form">report</a></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></li></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></ul></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="expando"</span><span style="color: silver;"> </span><span style="color: green;">style="display:</span><span style="color: silver;"> </span><span style="color: green;">none"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><span</span><span style="color: silver;"> </span><span style="color: green;">class="error">loading...</span></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="child"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="clearleft"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="thing</span><span style="color: silver;"> </span><span style="color: green;">id-t3_359t2l</span><span style="color: silver;"> </span><span style="color: green;">odd</span><span style="color: silver;"> </span><span style="color: green;">link</span><span style="color: silver;"> </span><span style="color: green;">self"</span><span style="color: silver;"> </span><span style="color: green;">onclick="click_thing(this)"</span><span style="color: silver;"> </span><span style="color: green;">data-fullname="t3_359t2l"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><p</span><span style="color: silver;"> </span><span style="color: green;">class="parent"></p></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><span</span><span style="color: silver;"> </span><span style="color: green;">class="rank">1</span></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="midcol</span><span style="color: silver;"> </span><span style="color: green;">unvoted"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="arrow</span><span style="color: silver;"> </span><span style="color: green;">up</span><span style="color: silver;"> </span><span style="color: green;">login-required"</span><span style="color: silver;"> </span><span style="color: green;">onclick="$(this).vote(r.config.vote_hash,</span><span style="color: silver;"> </span><span style="color: green;">null,</span><span style="color: silver;"> </span><span style="color: green;">event)"</span><span style="color: silver;"> </span><span style="color: green;">role="button"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">aria-label="upvote"</span><span style="color: silver;"> </span><span style="color: green;">tabindex="0"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="score</span><span style="color: silver;"> </span><span style="color: green;">likes">•</div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="score</span><span style="color: silver;"> </span><span style="color: green;">unvoted">•</div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="score</span><span style="color: silver;"> </span><span style="color: green;">dislikes">•</div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="arrow</span><span style="color: silver;"> </span><span style="color: green;">down</span><span style="color: silver;"> </span><span style="color: green;">login-required"</span><span style="color: silver;"> </span><span style="color: green;">onclick="$(this).vote(r.config.vote_hash,</span><span style="color: silver;"> </span><span style="color: green;">null,</span><span style="color: silver;"> </span><span style="color: green;">event)"</span><span style="color: silver;"> </span><span style="color: green;">role="button"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">aria-label="downvote"</span><span style="color: silver;"> </span><span style="color: green;">tabindex="0"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="entry</span><span style="color: silver;"> </span><span style="color: green;">unvoted"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><p</span><span style="color: silver;"> </span><span style="color: green;">class="title"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">class="title</span><span style="color: silver;"> </span><span style="color: green;">may-blank</span><span style="color: silver;"> </span><span style="color: green;">loggedin"</span><span style="color: silver;"> </span><span style="color: green;">href="/r/golang/comments/359t2l/bulding_api_services_in_go/"</span><span style="color: silver;"> </span><span style="color: green;">tabindex="1"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">rel="nofollow">Bulding</span><span style="color: silver;"> </span><span style="color: green;">API</span><span style="color: silver;"> </span><span style="color: green;">services</span><span style="color: silver;"> </span><span style="color: green;">in</span><span style="color: silver;"> </span><span style="color: green;">Go</a></span><span style="color: silver;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><span</span><span style="color: silver;"> </span><span style="color: green;">class="domain">(</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">href="/r/golang/">self.golang</a>)</span></p></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="expando-button</span><span style="color: silver;"> </span><span style="color: green;">collapsed</span><span style="color: silver;"> </span><span style="color: green;">selftext"</span><span style="color: silver;"> </span><span style="color: green;">onclick="expando_child(this)"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><p</span><span style="color: silver;"> </span><span style="color: green;">class="tagline">submitted</span><span style="color: silver;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><time</span><span style="color: silver;"> </span><span style="color: green;">title="Fri</span><span style="color: silver;"> </span><span style="color: green;">May</span><span style="color: silver;"> </span><span style="color: green;">8</span><span style="color: silver;"> </span><span style="color: green;">08:41:00</span><span style="color: silver;"> </span><span style="color: green;">2015</span><span style="color: silver;"> </span><span style="color: green;">UTC"</span><span style="color: silver;"> </span><span style="color: green;">datetime="2015-05-08T08:41:00+00:00"</span><span style="color: silver;"> </span><span style="color: green;">class="live-timestamp">a</span><span style="color: silver;"> </span><span style="color: green;">minute</span><span style="color: silver;"> </span><span style="color: green;">ago</time></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">by</span><span style="color: silver;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">href="http://www.reddit.com/user/jan1024188"</span><span style="color: silver;"> </span><span style="color: green;">class="author</span><span style="color: silver;"> </span><span style="color: green;">may-blank</span><span style="color: silver;"> </span><span style="color: green;">id-t2_8bbqm">jan1024188</a></p></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><ul</span><span style="color: silver;"> </span><span style="color: green;">class="flat-list</span><span style="color: silver;"> </span><span style="color: green;">buttons"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><li</span><span style="color: silver;"> </span><span style="color: green;">class="first"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">href="http://www.reddit.com/r/golang/comments/359t2l/bulding_api_services_in_go/"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">class="comments</span><span style="color: silver;"> </span><span style="color: green;">empty</span><span style="color: silver;"> </span><span style="color: green;">may-blank">comment</a></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></li></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><li</span><span style="color: silver;"> </span><span style="color: green;">class="share"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><span</span><span style="color: silver;"> </span><span style="color: green;">class="share-button</span><span style="color: silver;"> </span><span style="color: green;">toggle"</span><span style="color: silver;"> </span><span style="color: green;">style=""></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">class="option</span><span style="color: silver;"> </span><span style="color: green;">active</span><span style="color: silver;"> </span><span style="color: green;">login-required"</span><span style="color: silver;"> </span><span style="color: green;">href="#"</span><span style="color: silver;"> </span><span style="color: green;">tabindex="100"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">onclick="return</span><span style="color: silver;"> </span><span style="color: green;">toggle(this,</span><span style="color: silver;"> </span><span style="color: green;">share,</span><span style="color: silver;"> </span><span style="color: green;">cancelShare)">share</a></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">class="option"</span><span style="color: silver;"> </span><span style="color: green;">href="#">cancel</a></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></span></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></li></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><li</span><span style="color: silver;"> </span><span style="color: green;">class="link-save-button</span><span style="color: silver;"> </span><span style="color: green;">save-button"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">href="#">save</a></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></li></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><li></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><form</span><span style="color: silver;"> </span><span style="color: green;">action="/post/hide"</span><span style="color: silver;"> </span><span style="color: green;">method="post"</span><span style="color: silver;"> </span><span style="color: green;">class="state-button</span><span style="color: silver;"> </span><span style="color: green;">hide-button"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><input</span><span style="color: silver;"> </span><span style="color: green;">name="executed"</span><span style="color: silver;"> </span><span style="color: green;">value="hidden"</span><span style="color: silver;"> </span><span style="color: green;">type="hidden"</span><span style="color: silver;"> </span><span style="color: green;">/></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><span></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">href="javascript:void(0)"</span><span style="color: silver;"> </span><span style="color: green;">onclick="change_state(this,</span><span style="color: silver;"> </span><span style="color: green;">&#39;hide&#39;,</span><span style="color: silver;"> </span><span style="color: green;">hide_thing);">hide</a></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></span></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></form></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></li></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><li</span><span style="color: silver;"> </span><span style="color: green;">class="report-button"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><a</span><span style="color: silver;"> </span><span style="color: green;">href="javascript:void(0)"</span><span style="color: silver;"> </span><span style="color: green;">class="action-thing"</span><span style="color: silver;"> </span><span style="color: green;">data-action-form="#report-action-form">report</a></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></li></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></ul></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="expando"</span><span style="color: silver;"> </span><span style="color: green;">style="display:</span><span style="color: silver;"> </span><span style="color: green;">none"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><span</span><span style="color: silver;"> </span><span style="color: green;">class="error">loading...</span></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="child"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"><div</span><span style="color: silver;"> </span><span style="color: green;">class="clearleft"></div></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></div></span><span style="color: silver;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;"></body></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;"></html></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">`</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>strings<span style="color: black;">.</span>NewReader<span style="color: black;">(</span>s<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">checkErr(</span>err<span style="color: silver;"> </span><span style="color: navy;">error</span><span style="color: black;">,</span><span style="color: silver;"> </span>msg<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>log<span style="color: black;">.</span>Fatalln<span style="color: black;">(</span>msg<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">main()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>ExampleGoQuery<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<br />
Every day im falling more into love with Go!<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.crawlersmileys.com/img/smiley-write.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.crawlersmileys.com/img/smiley-write.jpg" /></a></div>
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com2tag:blogger.com,1999:blog-3324173739769750106.post-90230063309082277352015-05-07T08:23:00.001+00:002015-05-07T08:23:26.784+00:00Day 6 - String minifier, remove whitespacesWhile writing a webcrawler I stumbled over an annoying problem: The strings I did extract had CRLF, tabs and spaces in it which I did not want. So first I looked at the strings library. There are Replace and Trim functions, but none of them did exactly what I wanted: I did want to remove<i><b> </b></i>all unnecessary whitespaces, but not all of them, there should always be one single space between.<br />
<br />
So after googling for more than one hour I decided to write it myself and I was astonished that it took me only 5 minutes and it compiled at the first try (Go is such a great language!):<br />
<br />
<br /><span style="font-family: "Courier New",Courier,monospace;"><span style="font-size: x-small;">
</span></span><br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">package main </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">import</span><span style="color: silver;"> </span><span style="color: black;">(</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"strings"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"unicode"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">main()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>s<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: green;">"I</span><span style="color: silver;"> </span><span style="color: green;">am</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">string\n</span><span style="color: silver;"> </span><span style="color: green;">Containing</span><span style="color: silver;"> </span><span style="color: green;">tooo</span><span style="color: silver;"> </span><span style="color: green;">many</span><span style="color: silver;"> </span><span style="color: green;">spaces</span><span style="color: silver;"> </span><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">\n</span><span style="color: silver;"> </span><span style="color: green;">new</span><span style="color: silver;"> </span><span style="color: green;">lines"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy;">println</span><span style="color: black;">(</span><span style="color: green;">"Before:</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>s<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>s<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>stringMinifier<span style="color: black;">(</span>s<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy;">println</span><span style="color: black;">(</span><span style="color: green;">"After:</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>s<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy;">println</span><span style="color: black;">(</span><span style="color: green;">"----------\n"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>strings<span style="color: black;">.</span>Replace<span style="color: black;">(</span>s<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"am"</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"am</span><span style="color: silver;"> </span><span style="color: green;">not</span><span style="color: silver;"> </span><span style="color: green;">anymore"</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: purple;">1</span><span style="color: black;">))</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;"></span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;"> </span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">stringMinifier(</span>in<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">(</span>out<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span>
</pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>white<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy;">false</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">for</span><span style="color: silver;"> </span>_<span style="color: black;">,</span><span style="color: silver;"> </span>c<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">range</span><span style="color: silver;"> </span>in<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>unicode<span style="color: black;">.</span>IsSpace<span style="color: black;">(</span>c<span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span><span style="color: black;">!</span>white<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>out<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>out<span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: green;">"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>white<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span><span style="color: navy;">true</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">else</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>out<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>out<span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: black;">(</span>c<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>white<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span><span style="color: navy;">false</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;">
</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="font-family: Arial,Helvetica,sans-serif;">Output:</span><span style="color: black;">
</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">Before: I am a string
Containing tooo many spaces and
new lines
After: I am a string Containing tooo many spaces and new lines
----------
I am not anymore a string Containing tooo many spaces and new lines </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;"> </span></pre>
Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com5tag:blogger.com,1999:blog-3324173739769750106.post-87404173743747967692015-04-22T09:22:00.000+00:002015-05-14T17:43:19.625+00:00Day 5 - IPC using RPCAs RPC is built into Go, lets give it a try.<br />
<br />
Im using the rpcbotinterfaceobjects from Day 4 in <br />
<span style="font-family: "Courier New",Courier,monospace;">$GOPATH\src\github.com\<yourgithubusername>\rpcbotinterfaceobjects</span><br />
<span style="font-family: "Courier New",Courier,monospace;"></span><br />
If you dont have <span style="font-family: "Courier New",Courier,monospace;">rpcbotinterfaceobjects_input.go</span> and <span style="font-family: "Courier New",Courier,monospace;">rpcbotinterfaceobjects_output.go</span> please check my <a href="http://intogooglego.blogspot.co.at/2015/04/day-4-packages.html" target="_blank">Day 4</a> post.<br />
<br />
For RPC we need a new struct which connects the in&output structs to the RPC service:<br />
<br />
Create a new file <span style="font-family: "Courier New",Courier,monospace;">rpcservice.go</span> in <span style="font-family: "Courier New",Courier,monospace;">$GOPATH\src\github.com\<yourgithubusername>\rpcbotinterfaceobjects </span>and copy into it: <br />
<br />
<span style="color: navy; font-weight: 600;">package</span><span style="color: silver;"> </span>rpcbotinterfaceobjects
<br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">import</span><span style="color: silver;"> </span><span style="color: black;">(</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"strconv"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"strings"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">type</span><span style="color: silver;"> </span><span style="color: navy;">Bot</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">struct</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>ServiceCallCount<span style="color: silver;"> </span><span style="color: navy;">int</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">test</span><span style="color: silver;"> </span><span style="color: green;">ProcessPost</span><span style="color: silver;"> </span><span style="color: green;">function</span><span style="color: silver;"> </span><span style="color: green;">-</span><span style="color: silver;"> </span><span style="color: green;">turn</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">input</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">uppercase</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">(</span>this<span style="color: silver;"> </span><span style="color: black;">*</span><span style="color: navy;">Bot</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">ProcessPost(</span>in<span style="color: silver;"> </span><span style="color: black;">*</span>BotInput<span style="color: black;">,</span><span style="color: silver;"> </span>out<span style="color: silver;"> </span><span style="color: black;">*</span>BotOutput<span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: navy;">error</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>out<span style="color: black;">.</span>SetContent<span style="color: black;">(</span>strings<span style="color: black;">.</span>ToUpper<span style="color: black;">(</span>in<span style="color: black;">.</span>GetContent<span style="color: black;">())</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: green;">=</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>strconv<span style="color: black;">.</span>Itoa<span style="color: black;">(</span>this<span style="color: black;">.</span>ServiceCallCount<span style="color: black;">))</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>this<span style="color: black;">.</span>ServiceCallCount<span style="color: black;">++</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span><span style="color: navy;">nil</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<br />
<br />
<br />
Next is the server:<br />
<br />
In <span style="font-family: "Courier New",Courier,monospace;">$GOPATH\src\github.com\<yourgithubusername>\testrpc </span>create <span style="font-family: "Courier New",Courier,monospace;">botserver.go</span>:<br />
<br />
<span style="color: navy; font-weight: 600;">package</span><span style="color: silver;"> </span>main
<br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">import</span><span style="color: silver;"> </span><span style="color: black;">(</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"fmt"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"github.com/kimxilxyong/rpcbotinterfaceobjects"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"log"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"net"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"net/rpc"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"os"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"os/signal"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"runtime"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">init()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>runtime<span style="color: black;">.</span>GOMAXPROCS<span style="color: black;">(</span>runtime<span style="color: black;">.</span>NumCPU<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">main()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>InstallCtrlCPanic<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>InstallKillPanic<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>bot<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy;">new</span><span style="color: black;">(</span>rpcbotinterfaceobjects<span style="color: black;">.</span>Bot<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rpc<span style="color: black;">.</span>Register<span style="color: black;">(</span>bot<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>listener<span style="color: black;">,</span><span style="color: silver;"> </span>e<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>net<span style="color: black;">.</span>Listen<span style="color: black;">(</span><span style="color: green;">"tcp"</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">":9876"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>e<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>log<span style="color: black;">.</span>Fatal<span style="color: black;">(</span><span style="color: green;">"listen</span><span style="color: silver;"> </span><span style="color: green;">error:"</span><span style="color: black;">,</span><span style="color: silver;"> </span>e<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Println<span style="color: black;">(</span><span style="color: green;">"Server</span><span style="color: silver;"> </span><span style="color: green;">listening"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>rpc<span style="color: black;">.</span>Accept<span style="color: black;">(</span>listener<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">InstallCtrlCPanic</span><span style="color: silver;"> </span><span style="color: green;">installs</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">Ctrl-C</span><span style="color: silver;"> </span><span style="color: green;">signal</span><span style="color: silver;"> </span><span style="color: green;">handler</span><span style="color: silver;"> </span><span style="color: green;">that</span><span style="color: silver;"> </span><span style="color: green;">panics</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">InstallCtrlCPanic()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">go</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">func</span><span style="color: black;">()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">defer</span><span style="color: silver;"> </span>SavePanicTrace<span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>ch<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy;">make</span><span style="color: black;">(</span><span style="color: navy; font-weight: 600;">chan</span><span style="color: silver;"> </span>os<span style="color: black;">.</span>Signal<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: purple;">1</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>signal<span style="color: black;">.</span>Notify<span style="color: black;">(</span>ch<span style="color: black;">,</span><span style="color: silver;"> </span>os<span style="color: black;">.</span>Interrupt<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">for</span><span style="color: silver;"> </span>_<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">range</span><span style="color: silver;"> </span>ch<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy;">panic</span><span style="color: black;">(</span><span style="color: green;">"ctrl-c"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">InstallKillPanic</span><span style="color: silver;"> </span><span style="color: green;">installs</span><span style="color: silver;"> </span><span style="color: green;">a</span><span style="color: silver;"> </span><span style="color: green;">kill</span><span style="color: silver;"> </span><span style="color: green;">signal</span><span style="color: silver;"> </span><span style="color: green;">handler</span><span style="color: silver;"> </span><span style="color: green;">that</span><span style="color: silver;"> </span><span style="color: green;">panics</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">From</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">command-line,</span><span style="color: silver;"> </span><span style="color: green;">this</span><span style="color: silver;"> </span><span style="color: green;">signal</span><span style="color: silver;"> </span><span style="color: green;">is</span><span style="color: silver;"> </span><span style="color: green;">agitated</span><span style="color: silver;"> </span><span style="color: green;">with</span><span style="color: silver;"> </span><span style="color: green;">kill</span><span style="color: silver;"> </span><span style="color: green;">-ABRT</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">InstallKillPanic()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">go</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">func</span><span style="color: black;">()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//defer</span><span style="color: silver;"> </span><span style="color: green;">SavePanicTrace()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>ch<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy;">make</span><span style="color: black;">(</span><span style="color: navy; font-weight: 600;">chan</span><span style="color: silver;"> </span>os<span style="color: black;">.</span>Signal<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: purple;">1</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>signal<span style="color: black;">.</span>Notify<span style="color: black;">(</span>ch<span style="color: black;">,</span><span style="color: silver;"> </span>os<span style="color: black;">.</span>Kill<span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">for</span><span style="color: silver;"> </span>_<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">range</span><span style="color: silver;"> </span>ch<span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy;">panic</span><span style="color: black;">(</span><span style="color: green;">"sigkill"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">SavePanicTrace()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>r<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span><span style="color: navy;">recover</span><span style="color: black;">()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>r<span style="color: silver;"> </span><span style="color: black;">==</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Redirect</span><span style="color: silver;"> </span><span style="color: green;">stderr</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>file<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>os<span style="color: black;">.</span>Create<span style="color: black;">(</span><span style="color: green;">"panic"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy;">panic</span><span style="color: black;">(</span><span style="color: green;">"dumper</span><span style="color: silver;"> </span><span style="color: green;">(no</span><span style="color: silver;"> </span><span style="color: green;">file)</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>r<span style="color: black;">.(</span>fmt<span style="color: black;">.</span>Stringer<span style="color: black;">).</span>String<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//syscall.Dup2(int(file.Fd()),</span><span style="color: silver;"> </span><span style="color: green;">int(os.Stderr.Fd()))</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">defer</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">func</span><span style="color: black;">()</span><span style="color: silver;"> </span><span style="color: black;">{</span><span style="color: silver;"> </span>file<span style="color: black;">.</span>Close<span style="color: black;">()</span><span style="color: silver;"> </span><span style="color: black;">}()</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy;">panic</span><span style="color: black;">(</span><span style="color: green;">"dumper</span><span style="color: silver;"> </span><span style="color: green;">"</span><span style="color: silver;"> </span><span style="color: black;">+</span><span style="color: silver;"> </span>r<span style="color: black;">.(</span><span style="color: navy;">string</span><span style="color: black;">))</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<br />
<br />
<br />
<br />
Next is the client, in the same folder create: <span style="font-family: "Courier New",Courier,monospace;">botclient.go</span><br />
<br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">package</span><span style="color: silver;"> </span>main
<span style="color: navy; font-weight: 600;">import</span><span style="color: silver;"> </span><span style="color: black;">(</span>
<span style="color: silver;"> </span><span style="color: green;">"fmt"</span>
<span style="color: silver;"> </span><span style="color: green;">"github.com/kimxilxyong/rpcbotinterfaceobjects"</span>
<span style="color: silver;"> </span><span style="color: green;">"log"</span>
<span style="color: silver;"> </span><span style="color: green;">"net/rpc"</span>
<span style="color: black;">)</span>
<span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">main()</span><span style="color: silver;"> </span><span style="color: black;">{</span>
<span style="color: silver;"> </span>client<span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>rpc<span style="color: black;">.</span>Dial<span style="color: black;">(</span><span style="color: green;">"tcp"</span><span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: green;">"localhost:9876"</span><span style="color: black;">)</span>
<span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span>
<span style="color: silver;"> </span>log<span style="color: black;">.</span>Fatal<span style="color: black;">(</span><span style="color: green;">"dialing:"</span><span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: black;">)</span>
<span style="color: silver;"> </span><span style="color: black;">}</span>
<span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Synchronous</span><span style="color: silver;"> </span><span style="color: green;">call</span>
<span style="color: silver;"> </span>in<span style="color: silver;"> </span><span style="color: black;">:=</span><span style="color: silver;"> </span>rpcbotinterfaceobjects<span style="color: black;">.</span>BotInput<span style="color: black;">{</span><span style="color: green;">"BotInputTestText"</span><span style="color: black;">}</span>
<span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">var</span><span style="color: silver;"> </span>out<span style="color: silver;"> </span>rpcbotinterfaceobjects<span style="color: black;">.</span>BotOutput
<span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>client<span style="color: black;">.</span>Call<span style="color: black;">(</span><span style="color: green;">"Bot.ProcessPost"</span><span style="color: black;">,</span><span style="color: silver;"> </span>in<span style="color: black;">,</span><span style="color: silver;"> </span><span style="color: black;">&</span>out<span style="color: black;">)</span>
<span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">if</span><span style="color: silver;"> </span>err<span style="color: silver;"> </span><span style="color: black;">!=</span><span style="color: silver;"> </span><span style="color: navy;">nil</span><span style="color: silver;"> </span><span style="color: black;">{</span>
<span style="color: silver;"> </span>log<span style="color: black;">.</span>Fatal<span style="color: black;">(</span><span style="color: green;">"ProcessPost</span><span style="color: silver;"> </span><span style="color: green;">error:"</span><span style="color: black;">,</span><span style="color: silver;"> </span>err<span style="color: black;">)</span>
<span style="color: silver;"> </span><span style="color: black;">}</span>
<span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Printf<span style="color: black;">(</span><span style="color: green;">"ProcessPost:</span><span style="color: silver;"> </span><span style="color: green;">%s\n"</span><span style="color: black;">,</span><span style="color: silver;"> </span>out<span style="color: black;">.</span>GetContent<span style="color: black;">())</span>
<span style="color: black;">}</span>
</pre>
<br />
Now open a Cmd shell and build it:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">cd $GOPATH\src\github.com\<yourgithubusername>\testrpc</span> <br />
<span style="font-family: "Courier New",Courier,monospace;">go build botserver.go</span><br />
<span style="font-family: "Courier New",Courier,monospace;">go build botclient.go</span><br />
<br />
First run the server:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjt__mL_K0cq_urbB4klGrP1mOUPLCcnNVowHS4i3gIM2DQlyZDjsr7SQMeAGyFwFbOt7wjUj1-Zfgv3BbFH4Tr0poAd-Hti_YzivTNfneByKjpYfDSVXn7vyzTqgN82ZgO7f13c8f-83g/s1600/botserver.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="30" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjt__mL_K0cq_urbB4klGrP1mOUPLCcnNVowHS4i3gIM2DQlyZDjsr7SQMeAGyFwFbOt7wjUj1-Zfgv3BbFH4Tr0poAd-Hti_YzivTNfneByKjpYfDSVXn7vyzTqgN82ZgO7f13c8f-83g/s1600/botserver.png" width="320" /></a></div>
<br />
Then the client:<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidVUxtz-cqoURVeoPlYS6rnkVf6buex_vuJ6hJ6dXdHaIXdulbpgNIaMx5yf-C7Fi3mrQ_FxkS5zby4ROpod_9BLvFVpvtgwf-T_ajzH7HmwmTfnxnncp4zKMTjigQDFn23tlkljy08tI/s1600/botclient.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="106" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidVUxtz-cqoURVeoPlYS6rnkVf6buex_vuJ6hJ6dXdHaIXdulbpgNIaMx5yf-C7Fi3mrQ_FxkS5zby4ROpod_9BLvFVpvtgwf-T_ajzH7HmwmTfnxnncp4zKMTjigQDFn23tlkljy08tI/s1600/botclient.png" width="320" /></a></div>
<br />
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com1tag:blogger.com,1999:blog-3324173739769750106.post-23340456656190464052015-04-17T08:11:00.000+00:002015-05-14T17:44:08.473+00:00Day 4 - Go Packages<h2>
<span style="font-size: large;"><span style="font-weight: normal;">Today i want to check if my understandings of Go packages are correct:</span></span></h2>
(Reference: <a href="http://thenewstack.io/understanding-golang-packages/" target="_blank">golang-packages</a>)<br />
<br />
1) There is only one package inside a subdirectory under $GOPATH<br />
2) The package name has to be the same as the subdirectory name<br />
3) The package can consist of any number of files<br />
4) The filenames of the *.go files inside one package can be anything you choose<br />
<br />
<br />
So i created a new directory for my test library package:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">$GOPATH\src\github.com\<yourgithubusername>\rpcbotinterfaceobjects</span><br />
which is in my case:<br />
<span style="font-family: "Courier New",Courier,monospace;">D:\goworkspace\src\github.com\kimxilxyong\rpcbotinterfaceobjects</span><br />
<br />
<br />
Inside that directory i created two files:<br />
<br />
1) rpcbotinterfaceobjects_input.go<br />
<br />
<span style="color: navy; font-weight: 600;">package</span><span style="color: silver;"> </span>rpcbotinterfaceobjects
<br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">type</span><span style="color: silver;"> </span><span style="color: navy;">BotInput</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">struct</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Content<span style="color: silver;"> </span><span style="color: navy;">string</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">(</span>this<span style="color: silver;"> </span><span style="color: black;">*</span><span style="color: navy;">BotInput</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">GetContent()</span><span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>this<span style="color: black;">.</span>Content</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">(</span>this<span style="color: silver;"> </span><span style="color: black;">*</span><span style="color: navy;">BotInput</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">SetContent(</span>NewContent<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>this<span style="color: black;">.</span>Content<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>NewContent</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<br />
2) rpcbotinterfaceobjects_output.go<br />
<br />
<span style="color: navy; font-weight: 600;">package</span><span style="color: silver;"> </span>rpcbotinterfaceobjects
<br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">type</span><span style="color: silver;"> </span><span style="color: navy;">BotOutput</span><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">struct</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>Content<span style="color: silver;"> </span><span style="color: navy;">string</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">(</span>this<span style="color: silver;"> </span><span style="color: black;">*</span><span style="color: navy;">BotOutput</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">GetContent()</span><span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">return</span><span style="color: silver;"> </span>this<span style="color: black;">.</span>Content</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">(</span>this<span style="color: silver;"> </span><span style="color: black;">*</span><span style="color: navy;">BotOutput</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">SetContent(</span>NewContent<span style="color: silver;"> </span><span style="color: navy;">string</span><span style="color: black;">)</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>this<span style="color: black;">.</span>Content<span style="color: silver;"> </span><span style="color: black;">=</span><span style="color: silver;"> </span>NewContent</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">
</span></pre>
As you can see the package names are both the same, but the filenames can be anything you choose.<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihC-d45pUiBq2p5P8Ky1DDeiMO28zaz97iid7caGP7eWBpQBVQ9KqOQRCZT8y-Da2MUe-zDNlu9yYi-zdvt0ezdSkKbnknjz6oyctVZLPRkyAiSOaV5oOlQIhnGe5eQIbIiXtqGjksjpI/s1600/packages.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihC-d45pUiBq2p5P8Ky1DDeiMO28zaz97iid7caGP7eWBpQBVQ9KqOQRCZT8y-Da2MUe-zDNlu9yYi-zdvt0ezdSkKbnknjz6oyctVZLPRkyAiSOaV5oOlQIhnGe5eQIbIiXtqGjksjpI/s1600/packages.png" width="320" /></a></div>
<br />
<br />
<br />
<h3>
<span style="font-weight: normal;">Next is to create the executable which uses this package in: </span></h3>
<span style="font-family: "Courier New",Courier,monospace;">$GOPATH\src\github.com\<yourgithubusername>\packagetest</span> <br />
or in my case:<br />
<span style="font-family: "Courier New",Courier,monospace;">D:\goworkspace\src\github.com\kimxilxyong\packagetest</span><br />
<br />
Create a file named test_my_package.go (or actually any name you want) and insert the following code into it:<br />
<br />
<span style="color: navy; font-weight: 600;">package</span><span style="color: silver;"> </span>main
<br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">import</span><span style="color: silver;"> </span><span style="color: black;">(</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"fmt"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: green;"> // use the rpcbotinterfaceobjects package</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">"github.com/kimxilxyong/rpcbotinterfaceobjects"</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">main()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Variable</span><span style="color: silver;"> </span><span style="color: green;">declaration</span><span style="color: silver;"> </span><span style="color: green;">for</span><span style="color: silver;"> </span><span style="color: green;">in</span><span style="color: silver;"> </span><span style="color: green;">and</span><span style="color: silver;"> </span><span style="color: green;">output objects</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">var</span><span style="color: silver;"> </span>input<span style="color: silver;"> </span>rpcbotinterfaceobjects<span style="color: black;">.</span>BotInput</pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: navy; font-weight: 600;">var</span><span style="color: silver;"> </span>output<span style="color: silver;"> </span>rpcbotinterfaceobjects<span style="color: black;">.</span>BotOutput</pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Write</span><span style="color: silver;"> </span><span style="color: green;">test</span><span style="color: silver;"> </span><span style="color: green;">strings</span><span style="color: silver;"> </span><span style="color: green;">to</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">objects</span><span style="color: green;"></span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>input<span style="color: black;">.</span>SetContent<span style="color: black;">(</span><span style="color: green;">"rpcbotinterfaceobjects.Input_Content.test_my_package"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>output<span style="color: black;">.</span>SetContent<span style="color: black;">(</span><span style="color: green;">"rpcbotinterfaceobjects.Output_Content.test_my_package"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span><span style="color: green;">//</span><span style="color: silver;"> </span><span style="color: green;">Show</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">content</span><span style="color: silver;"> </span><span style="color: green;">of</span><span style="color: silver;"> </span><span style="color: green;">the</span><span style="color: silver;"> </span><span style="color: green;">objects</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Println<span style="color: black;">(</span>input<span style="color: black;">.</span>GetContent<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Println<span style="color: black;">(</span>output<span style="color: black;">.</span>GetContent<span style="color: black;">())</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<br />
Open a cmd shell and cd into the <span style="font-family: "Courier New",Courier,monospace;">packagetest</span> directory.<br />
<br />
Run <span style="font-family: "Courier New",Courier,monospace;">go install</span> to build the executable, it will be put into your <span style="font-family: "Courier New",Courier,monospace;">$GOPATH\bin</span> folder.<br />
<br />
Note: The bin will be named after your folder, not your *.go file!<br />
<br />
Run <span style="font-family: "Courier New",Courier,monospace;">packagetest</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBhOhACNUw8GQGK0GGOr5YR7xBiWAs-EKzIFGTFUPVDMKKxuNvbTCC4Qfx3gREIXib26DFLpN5DpCoP1j1LKkV2HBgLhCmENfL_mjE7xHaOkNfWFP7ftLBJKRQ4WwfDOEZHxdPmFikpro/s1600/package_test_main.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="191" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBhOhACNUw8GQGK0GGOr5YR7xBiWAs-EKzIFGTFUPVDMKKxuNvbTCC4Qfx3gREIXib26DFLpN5DpCoP1j1LKkV2HBgLhCmENfL_mjE7xHaOkNfWFP7ftLBJKRQ4WwfDOEZHxdPmFikpro/s1600/package_test_main.png" width="320" /></a></div>
<br />
<br />
<br />
So far my assumptions 1-4 have been confirmed and additionally my tests seem to indicate that if you compile a file with <span style="font-family: "Courier New",Courier,monospace;">func main()</span> in it, the resulting exe is named like the directory your main comes from.<br />
<br />
Maybe some of you may ask why I used <span style="font-family: "Courier New",Courier,monospace;">this</span> in the struct methods. Thats because I come from a Java and Delphi background and as <span style="font-family: "Courier New",Courier,monospace;">this</span> or <span style="font-family: "Courier New",Courier,monospace;">self</span> is not a keyword in Go I just used it for better readability.<br />
<br />
Quote:<br />
<i>Golang does not have a self or this keyword to reference to the current instance. In the method example of <b>func (c *Circle) area() float64</b> the receiver struct is named "c". Use that variable name, rather than this to refer to the current instance.</i><br />
<a href="http://lexsheehan.blogspot.co.at/2014/07/oo-example-in-golang.html" target="_blank">oo-example-in-golang</a><br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com0tag:blogger.com,1999:blog-3324173739769750106.post-49131883804792395692015-04-16T05:19:00.000+00:002015-04-22T08:09:35.684+00:00Day 3 - The famous Hello<h2>
<span style="font-weight: normal;"><span style="font-size: large;">Today I want to test if all my previous preperations where correct and create my first runnable Go programm. </span></span></h2>
<br />
Create a new directory for the hello world test code inside your workspace:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">$GOPATH\src\github.com\<yourgithubusername>\hello</span><br />
In my case this is:<br />
<span style="font-family: "Courier New",Courier,monospace;">D:\goworkspace\src\github.com\kimxilxyong\hello</span><br />
<br />
Inside the <span style="font-family: "Courier New",Courier,monospace;">hello</span> directory create a file named <span style="font-family: "Courier New",Courier,monospace;">hello.go</span> and copy the following code into it:<br />
<br />
<span style="color: navy; font-weight: 600;">package</span><span style="color: silver;"> </span>main
<br />
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">import</span><span style="color: silver;"> </span><span style="color: green;">"fmt"</span></pre>
<pre style="-qt-block-indent: 0; -qt-paragraph-type: empty; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: navy; font-weight: 600;">func</span><span style="color: silver;"> </span><span style="color: black;">main()</span><span style="color: silver;"> </span><span style="color: black;">{</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: silver;"> </span>fmt<span style="color: black;">.</span>Printf<span style="color: black;">(</span><span style="color: green;">"Hello,</span><span style="color: silver;"> </span><span style="color: green;">world. Heureka!\n"</span><span style="color: black;">)</span></pre>
<pre style="-qt-block-indent: 0; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-indent: 0px;"><span style="color: black;">}</span></pre>
<br />
One important point here is that the opening curly brace MUST be on the same line as <span style="font-family: "Courier New",Courier,monospace;">func main</span>! (Thats some Go specific syntax, see: <a href="http://stackoverflow.com/questions/17153838/why-does-golang-enforce-curly-bracket-to-not-be-on-the-next-line" target="_blank">Curly brace in Go</a>)<br />
<br />
You can edit the code with any editor you like, i am using LiteIDE which is a <i>"simple, open source, cross-platform Go IDE". </i><br />
<a href="http://sourceforge.net/projects/liteide" target="_blank">http://sourceforge.net/projects/liteide</a><br />
<br />
Next open a cmd prompt and cd into<span style="font-family: "Courier New",Courier,monospace;"> </span><br />
<span style="font-family: "Courier New",Courier,monospace;">$GOPATH\src\github.com\<yourgithubusername>\hello</span><br />
<br />
Run "<span style="font-family: "Courier New",Courier,monospace;">go install</span>" to compile, this will put the runable hello binary into <span style="font-family: "Courier New",Courier,monospace;">$GOPATH\bin</span><br />
(no output is good, means no error occured) <br />
<br />
Run "<span style="font-family: "Courier New",Courier,monospace;">hello</span>" and hope ... <br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRRktmlGtN-zyLoJYYtxl5GIsbMjjFYY7bHzfxB6rTXgIzXb6qXTeNHArF38fDqQrNNKuyIB46z2-gbJbfehAKOvQjAc95UVO3uJxmad-bKkCOubYMgAfTAAFPlFRv4LgYIs31XrGzwnA/s1600/smiley_question.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRRktmlGtN-zyLoJYYtxl5GIsbMjjFYY7bHzfxB6rTXgIzXb6qXTeNHArF38fDqQrNNKuyIB46z2-gbJbfehAKOvQjAc95UVO3uJxmad-bKkCOubYMgAfTAAFPlFRv4LgYIs31XrGzwnA/s1600/smiley_question.png" /></a></div>
<br />
<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmEMRe9v8lse3D4SXi3z6AZleBqFaeuQilu1Ir-iHFsdafoplOT17Shn3gN-LT11yNXdTcQtRm0HcKz0iSrf755m8ar-p8w34CgAaH1bpQQprkp3lqowrjfSEYdQ4KPOvI3C61gtx9AsA/s1600/hello.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmEMRe9v8lse3D4SXi3z6AZleBqFaeuQilu1Ir-iHFsdafoplOT17Shn3gN-LT11yNXdTcQtRm0HcKz0iSrf755m8ar-p8w34CgAaH1bpQQprkp3lqowrjfSEYdQ4KPOvI3C61gtx9AsA/s1600/hello.png" height="217" width="320" /></a></div>
<br />
<br />
Aaaand ... it worked, <span style="font-size: large;">Heureka</span> !!<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMKJPIYE1Z2ncwVidY-Gi82yL3RDHEdZq8RU8fKDyLThHB4vNkYdLVwH0ztHFXzf0pTjKeru8RoCKug660S5mXETqZK_oW1AvxMQt0ufB4Cukxp7DnyVYiIzsNniExWDiodeSWiHfxywc/s1600/awesome.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMKJPIYE1Z2ncwVidY-Gi82yL3RDHEdZq8RU8fKDyLThHB4vNkYdLVwH0ztHFXzf0pTjKeru8RoCKug660S5mXETqZK_oW1AvxMQt0ufB4Cukxp7DnyVYiIzsNniExWDiodeSWiHfxywc/s1600/awesome.png" /></a></div>
<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKDodlTA4gi6TxYSxavvAdyFmWPNVNBm97SRBHnywqm5HxpsyZak_KprPhqc_9mkLkgVUY_38R0Swd7YPK_95ESFxddgfoxvblvpUj3H5I32kNuEVqv2ZLoHjtL9j6i1J9C7oUcpzZSv4/s1600/hello.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><br /></a></div>
Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com0tag:blogger.com,1999:blog-3324173739769750106.post-5870193237258774142015-04-15T04:01:00.000+00:002015-05-14T17:41:54.651+00:00Day 2 - Creating a Go workspace<h2>
<span style="font-weight: normal;"><span style="font-size: large;">Creating a Go workspace was much more "head scratching" for me than I thought it would be.</span></span></h2>
<br />
There is this github folder thing in your workspace and the enviroment variables to set which I did not understand at the first glance. So here is what I googled and did to set up a workspace:<br />
<br />
Create a simple empty Directory which is NOT the same as $GOROOT and point $GOPATH to it:<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeub-yWVo6NZAVuJqMsXh4bpMnfBFTD5wrfTeM_xwn3hFA6kKzEohJpZIcrdJf5oIIwwzuJvoJ8E7Ld3xFXyezuD_llHU9xfwTvAHTdSfYbKrK3oDONC1bRG3LUnlIce6tytd2LpBjAkM/s1600/gopath.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="71" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeub-yWVo6NZAVuJqMsXh4bpMnfBFTD5wrfTeM_xwn3hFA6kKzEohJpZIcrdJf5oIIwwzuJvoJ8E7Ld3xFXyezuD_llHU9xfwTvAHTdSfYbKrK3oDONC1bRG3LUnlIce6tytd2LpBjAkM/s400/gopath.png" width="400" /></a></div>
<br />
(How to set Environment variables in Windows: <a href="http://www.computerhope.com/issues/ch000549.htm" target="_blank">http://www.computerhope.com/issues/ch000549.htm</a>) Type "<span style="font-family: "Courier New",Courier,monospace;">set</span>" in a cmd window to show them.<br />
<br />
Create a subdirectory "<span style="font-family: "Courier New",Courier,monospace;">src</span>" in $GOPATH ( <span style="font-family: "Courier New",Courier,monospace;">D:\goworkspace</span> in my case )<br />
<br />
Create the following path: <span style="font-family: "Courier New",Courier,monospace;">$GOPATH\src\github.com\<yourgithubusername></span><br />
<br />
The github subdirectory is not mandatory but is considered best practice and will make things easier for you in the future if you want to store your source on github. You dont need to have a github account at this time, just use a username that you would be using later on.<br />
<br />
Reference: <a href="http://golang.org/doc/code.html#Workspaces" target="_blank">http://golang.org/doc/code.html#Workspaces</a><br />
<br />
<br />
The source directory for all my Go code is now:<br />
<br />
<span style="font-family: "Courier New", Courier, monospace;">D:\goworkspace\src\github.com\kimxilxyong</span> <br />
<br />
There are different opinions out there on the interwebs, but as far as I have googled it, it is recommended that you have only ONE workspace. That means all your own source and 3rd party source is all together in one workspace. I will adhere to this recommendation for now, but would be glad to read your opinions and/or arguments about this question.<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com0tag:blogger.com,1999:blog-3324173739769750106.post-70807427832516239402015-04-14T08:23:00.000+00:002015-05-08T15:39:55.587+00:00Day 1 - Installing the Go runtime on Windows<h2>
<span style="font-size: large;"><span style="font-weight: normal;">This is the first day of my journey into the programming language Go from Google (golang).</span></span></h2>
<br />
I have choosen Go to be the next language im going to learn because it is the best of two worlds: It is as fast as a compiled language, but feels like an interpreted one.<br />
<a href="https://golang.org/doc/faq" target="_blank">https://golang.org/doc/faq</a><br />
<br />
How to getting started with GoLang on Windows7 64bit:<br />
<br />
Go to <a href="http://golang.org/dl" target="_blank">http://golang.org/dl</a> and download the msi, then install it<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjV4OBK1iYGjQgPEriJ92CCLMpTy_TXiBfzuw6BpYFy1DDOkdM9y5lUzuN2VSmV4tBY7lGiiSOZu3_3Gbw4b306ljWDc_VVfnP4kJBJoRHUHrOPhiU9FZiKbIvg2-X8YLKbleExTgams2I/s1600/install.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="153" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjV4OBK1iYGjQgPEriJ92CCLMpTy_TXiBfzuw6BpYFy1DDOkdM9y5lUzuN2VSmV4tBY7lGiiSOZu3_3Gbw4b306ljWDc_VVfnP4kJBJoRHUHrOPhiU9FZiKbIvg2-X8YLKbleExTgams2I/s640/install.png" width="640" /></a></div>
<br />
<br />
Check if it has been installed correctly by starting a cmd shell and enter "go version", it should show something like this:<br />
<span style="font-size: x-small;"><br /></span>
<span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;">C:\>go version<br />go version go1.4.2 windows/amd64</span></span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXrTPel8Fi5pAQSIZD4JUMkn0YylI1k3wkFln6rGspHXStWBZj5x_NDMpoQuJuQkiD8hi9eGlpRRZX1f8Op_9fzgHlpUzIW4mqS7H9qZ33-VcRXbe7hytls32WUhRTmF37LcLGk3iG3yw/s1600/test_install.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="26" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXrTPel8Fi5pAQSIZD4JUMkn0YylI1k3wkFln6rGspHXStWBZj5x_NDMpoQuJuQkiD8hi9eGlpRRZX1f8Op_9fzgHlpUzIW4mqS7H9qZ33-VcRXbe7hytls32WUhRTmF37LcLGk3iG3yw/s400/test_install.png" width="400" /></a></div>
<br />
<br />
The environment variable $GOROOT should have been created and $PATH should include $GOROOT\bin.<br />
<br />
Type "<span style="font-family: "Courier New",Courier,monospace;">set</span>" to check if these variables are correct if "<span style="font-family: "Courier New",Courier,monospace;">go version</span>" gives you an error.<br />
<br />
To display only one environment variable:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">C:\>echo %GOROOT%<br />C:\Go\ </span><br />
<br />
and/or<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">C:\>echo %PATH%<br /><some_path_list>;C:\Go\bin;<some_other_pathes> </span><br />
<br />
How to set Environment variables in Windows: <a href="http://www.computerhope.com/issues/ch000549.htm" target="_blank">http://www.computerhope.com/issues/ch000549.htm</a> <br />
<br />
<br />
Im sure you already know him but im posting him nevertheless because he is the cutest mascot since <a href="http://en.wikipedia.org/wiki/Tux" target="_blank">Tux</a>.<br />
<br />
Please meet Gopher, the GoLang mascot:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjIaMpQrkBr5zw6rMYP5NuPA63wKBBEivdGNFHGc4KGGpPiorMH6OCxfMS3ZXPsWOx_5-quYjgW6cOVJUhLu9_NlaOlO_1rHs_P1eu-feU6VQ6V4f1DmdsSbURZLm0IMpFZlky3xMGRJ8/s1600/gopher.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjIaMpQrkBr5zw6rMYP5NuPA63wKBBEivdGNFHGc4KGGpPiorMH6OCxfMS3ZXPsWOx_5-quYjgW6cOVJUhLu9_NlaOlO_1rHs_P1eu-feU6VQ6V4f1DmdsSbURZLm0IMpFZlky3xMGRJ8/s1600/gopher.png" /></a></div>
(Downloaded the png from <a href="http://qiita.com/" target="_blank">qiita.com</a>)<br />
<br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05779040593006264728noreply@blogger.com2