tag:blogger.com,1999:blog-1585885952972099742024-02-20T21:44:39.300+01:00Go Daily TipsAnonymoushttp://www.blogger.com/profile/05438732025999671828noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-158588595297209974.post-20505194121710557532015-01-15T22:10:00.002+01:002015-01-15T22:10:40.869+01:00Multiple switch casesSwitch cases can be separated by coma:<br />
<br /><span style="font-family: "Courier New",Courier,monospace;"> func shouldEscape(c byte) bool {<br /> switch c {<br /> case ' ', '?', '&', '=', '#', '+', '%':<br /> return true<br /> }<br /> return false<br /> }</span>Anonymoushttp://www.blogger.com/profile/05438732025999671828noreply@blogger.com0tag:blogger.com,1999:blog-158588595297209974.post-49485892477825137282015-01-11T11:07:00.002+01:002015-01-11T11:07:19.294+01:00Alternative to labelGet out of a for/select loop without using labels:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">func main() {<br /> </span><br />
<span style="font-family: "Courier New",Courier,monospace;">OUT:<br /> for {<br /> select {<br /> case ... ):<br /> ...<br /> default:<br /> break OUT<br /> }<br /> }<br /> ....<br />}</span><br />
<br />
Refactoring out the for loop:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">func main() {</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> ...<br /> doSomething()<br /> ...<br />}<br /><br />func doSomething() {<br /> for {<br /> select {<br /> case ...:<br /> ...<br /> default:<br /> return<br /> }<br /> }<br />}</span>Anonymoushttp://www.blogger.com/profile/05438732025999671828noreply@blogger.com0tag:blogger.com,1999:blog-158588595297209974.post-1463313135903881582015-01-10T19:43:00.002+01:002015-01-10T19:45:54.912+01:00Basic dependency management<div class="rule">
To install all the dependencies of a package:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">$ go list -f '{{ join .Imports "\n" }}' | xargs go get </span></div>
Anonymoushttp://www.blogger.com/profile/05438732025999671828noreply@blogger.com0tag:blogger.com,1999:blog-158588595297209974.post-55840294292058545802015-01-08T18:30:00.006+01:002015-01-11T09:10:02.051+01:00TestMain in go 1.4 testing module<br />
<div class="line" id="file-testmain_example_test-go-LC14">
Provide setup and teardow to your test with g0 1.4 testing module:</div>
<br />
<br />
<div class="line" id="file-testmain_example_test-go-LC14">
<span style="font-family: "Courier New",Courier,monospace;"><span class="pl-k"><span style="font-family: "Courier New",Courier,monospace;"><span class="pl-k"> </span></span></span></span><br />
<span style="font-family: "Courier New",Courier,monospace;"><span class="pl-k">func</span> <span class="pl-en">setUp</span>() {
</span></div>
<br />
<div class="line" id="file-testmain_example_test-go-LC15">
<span style="font-family: "Courier New",Courier,monospace;"> ...</span>
</div>
<div class="line" id="file-testmain_example_test-go-LC16">
<span style="font-family: "Courier New",Courier,monospace;">}
</span></div>
<div class="line" id="file-testmain_example_test-go-LC17">
<span style="font-family: "Courier New",Courier,monospace;">
</span></div>
<div class="line" id="file-testmain_example_test-go-LC18">
<span style="font-family: "Courier New",Courier,monospace;"><span class="pl-k">func</span> <span class="pl-en">tearDown</span>() {
</span></div>
<div class="line" id="file-testmain_example_test-go-LC19">
<span style="font-family: "Courier New",Courier,monospace;"> ...</span>
</div>
<div class="line" id="file-testmain_example_test-go-LC20">
<span style="font-family: "Courier New",Courier,monospace;">}
</span></div>
<div class="line" id="file-testmain_example_test-go-LC21">
<span style="font-family: "Courier New",Courier,monospace;"><br /></span></div>
<div class="line" id="file-testmain_example_test-go-LC22">
<span style="font-family: "Courier New",Courier,monospace;"><span class="pl-k">func</span> <span class="pl-en">TestMain</span>(<span class="pl-v">m</span> *<span class="pl-v">testing</span>.<span class="pl-v">M</span>) {
</span></div>
<div class="line" id="file-testmain_example_test-go-LC23">
<span style="font-family: "Courier New",Courier,monospace;"> <span class="pl-s3">setUp</span>()
</span></div>
<div class="line" id="file-testmain_example_test-go-LC24">
<span style="font-family: "Courier New",Courier,monospace;"> <span class="pl-vo">r</span> <span class="pl-k">:=</span> m.<span class="pl-s3">Run</span>()
</span></div>
<div class="line" id="file-testmain_example_test-go-LC25">
</div>
<div class="line" id="file-testmain_example_test-go-LC26">
<span style="font-family: "Courier New",Courier,monospace;"> <span class="pl-s3">tearDown</span>() // cannot run tearDown in a defer because of os.Exit
</span></div>
<div class="line" id="file-testmain_example_test-go-LC28">
<span style="font-family: "Courier New",Courier,monospace;"> os.<span class="pl-s3">Exit</span>(r)
</span></div>
<div class="line" id="file-testmain_example_test-go-LC29">
<span style="font-family: "Courier New",Courier,monospace;">} </span></div>
<pre class="line-pre"><span style="font-family: "Courier New",Courier,monospace;">
</span></pre>
<div class="line" id="file-testmain_example_test-go-LC8">
<span style="font-family: "Courier New",Courier,monospace;"><span class="pl-k">func</span> <span class="pl-en">TestFoo</span>(<span class="pl-v">t</span> *<span class="pl-v">testing</span>.<span class="pl-v">T</span>) { </span><br />
<span style="font-family: "Courier New",Courier,monospace;"> ... </span></div>
<div class="line" id="file-testmain_example_test-go-LC9">
<span style="font-family: "Courier New",Courier,monospace;">}
</span></div>
<div class="line" id="file-testmain_example_test-go-LC10">
<span style="font-family: "Courier New",Courier,monospace;">
</span></div>
<div class="line" id="file-testmain_example_test-go-LC11">
<span style="font-family: "Courier New",Courier,monospace;"><span class="pl-k">func</span> <span class="pl-en">TestB</span>ar(<span class="pl-v">t</span> *<span class="pl-v">testing</span>.<span class="pl-v">T</span>) { </span><br />
<span style="font-family: "Courier New",Courier,monospace;"> ... </span></div>
<div class="line" id="file-testmain_example_test-go-LC12">
<span style="font-family: "Courier New",Courier,monospace;">}
</span></div>
<div class="line" id="file-testmain_example_test-go-LC13">
<span style="font-family: "Courier New",Courier,monospace;">
</span></div>
Anonymoushttp://www.blogger.com/profile/05438732025999671828noreply@blogger.com0tag:blogger.com,1999:blog-158588595297209974.post-21104624202132452762015-01-07T12:04:00.001+01:002015-01-07T12:31:25.382+01:00Exiting carefully<span style="font-family: "Courier New",Courier,monospace;">os.Exit()</span> terminates immediately and deferred functions are
not run.
Anonymoushttp://www.blogger.com/profile/05438732025999671828noreply@blogger.com0tag:blogger.com,1999:blog-158588595297209974.post-32191889258006374662015-01-06T10:13:00.000+01:002015-01-07T12:31:43.522+01:00Notes on expressionsExpressions<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">"x |= y" is equivalent to "x = x | y", just like "x += y" is equivalent to "x = x + y".</span><br />
<br />
Same for <span style="font-family: "Courier New",Courier,monospace;">&=</span> .Anonymoushttp://www.blogger.com/profile/05438732025999671828noreply@blogger.com0tag:blogger.com,1999:blog-158588595297209974.post-68758666975703513192015-01-05T00:01:00.000+01:002015-01-05T00:02:24.808+01:00Check interface implementation- Check if a variable implements a given interface and obtain the variable in the context of that interface:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">m, ok := val.(json.Marshaler)</span><br />
<br />
<br />
-For example:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;"> if _, ok := val.(json.Marshaler); ok {<br /> fmt.Printf("value %v of type %T implements json.Marshaler\n", val, val)<br /> }</span>
Anonymoushttp://www.blogger.com/profile/05438732025999671828noreply@blogger.com0tag:blogger.com,1999:blog-158588595297209974.post-19420658139672298522015-01-04T00:57:00.000+01:002015-01-04T23:58:18.947+01:00Select For and Loops- The canonical way to block indefinitely is an empty select statement:<br />
<span style="font-family: "Courier New",Courier,monospace;">select{}</span><br />
<br />
- While canonical way to loop indefinitely is an empty for statement:<br />
<span style="font-family: "Courier New",Courier,monospace;">for{}</span>Anonymoushttp://www.blogger.com/profile/05438732025999671828noreply@blogger.com0tag:blogger.com,1999:blog-158588595297209974.post-71248173808102442422015-01-04T00:56:00.000+01:002015-01-04T00:56:58.464+01:00Trim strings- To remove final nulls from strings coming from a buffer:<br />
<span style="font-family: "Courier New",Courier,monospace;"> strings.TrimRight(myStringFromABuffer, "\x00")</span><br />
<br />
- To remove new lines from string:<br />
<span style="font-family: "Courier New",Courier,monospace;"> strings.Trim(myString, "\r\n")</span>Anonymoushttp://www.blogger.com/profile/05438732025999671828noreply@blogger.com0tag:blogger.com,1999:blog-158588595297209974.post-61017007677873779852015-01-02T16:01:00.000+01:002015-01-02T16:51:32.510+01:00Go Channels Tips- Read from an unbuffered channel:<br />
blocks (use select).<br />
<br />
- Write to an unbuffered channel:<br />
blocks.<br />
<br />
- Read from a buffered channel:<br />
blocks (use select).<br />
<br />
- Write to an buffered channel:<br />
non blocking as long as there is space left in the buffer.<br />
<br />
- Read from a closed channel:<br />
non blocking, always return the zero value of the channel type.<br />
<br />
- Write to a closed channel:<br />
panic: runtime error: send on closed channel<br />
<br />
- Read from a nil channel:<br />
blocks forever.<br />
<br />
- Write to a nil channel:<br />
blocks forever.Anonymoushttp://www.blogger.com/profile/05438732025999671828noreply@blogger.com0