<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Thuc Le on Medium]]></title>
        <description><![CDATA[Stories by Thuc Le on Medium]]></description>
        <link>https://medium.com/@thuc?source=rss-6b6df8950fc3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*TUZwZtaQNffc_EQaZvm7ww.png</url>
            <title>Stories by Thuc Le on Medium</title>
            <link>https://medium.com/@thuc?source=rss-6b6df8950fc3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 05:29:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@thuc/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[7 notes about strings.builder in Golang]]></title>
            <link>https://medium.com/@thuc/8-notes-about-strings-builder-in-golang-65260daae6e9?source=rss-6b6df8950fc3------2</link>
            <guid isPermaLink="false">https://medium.com/p/65260daae6e9</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[builders]]></category>
            <category><![CDATA[string]]></category>
            <dc:creator><![CDATA[Thuc Le]]></dc:creator>
            <pubDate>Sat, 24 Mar 2018 18:10:49 GMT</pubDate>
            <atom:updated>2018-08-16T10:48:30.735Z</atom:updated>
            <content:encoded><![CDATA[<p>A month since Go 1.10 was released, I have a little time to work with strings.Builder and take some note of it. You maybe know about them, especially if you are familiar with bytes.Buffer. So I share them and hope they are useful for you</p><h3>1. Four forms of write-method</h3><p>Such as bytes.Buffer, the strings.Builder supports four methods to write data to the builder:</p><pre>func (b *Builder) Write(p []byte) (int, error)<br>func (b *Builder) WriteByte(c byte) error<br>func (b *Builder) WriteRune(r rune) (int, error)<br>func (b *Builder) WriteString(s string) (int, error)</pre><p>With four forms of writing methods, developers can select the appropriate method with input data: list of bytes, a byte, a rune or a string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IGv0x1gMwkszbv7IWnpfaQ.png" /></figure><h3>2. Way to store string data</h3><p>From library usage view, we call write-methods of string.Builder to write content and call String() to get the accumulated string. But how does string.Builder organize content?</p><blockquote>The slice</blockquote><p>The string.Builder uses an internal slice to store pieces of data. When developer call write-methods to write content, the slice will be appended internally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*luRaetJ4m36JH43xh0rHcA.png" /></figure><h3>3. Using strings.Builder effectively</h3><p>As you known in the 2nd note, strings.Builder organizes the content based on the internal slice to organize. When you call write-methods, they append new bytes to inner-slice. If the slice’s capacity is reached, Go will allocate a new slice with different memory space and copy old slice to a new one. It will take resource to do when the slice is large or it may create <a href="https://blog.siliconstraits.com/out-of-memory-with-append-in-golang-956e7eb2c70e">the memory issue</a>. We should try to avoid it string.Builder</p><p>Regarding slice, golang support make([]TypeOfSlice, length, capacity) to pre-define the capacity to use. It avoids to reach the max of capacity and extends.</p><p>The strings.Builder also supports a method to pre-define the capacity before using, the Grow(). When we can pre-define the capacity we assume to use, the strings.Builder avoids allocating new slice to extends capacity.</p><pre>func (b *Builder) Grow(n int)</pre><p>When calling Grow(), we must define a number of bytes (n) that we want extends for capacity. The Grow() method make sure the Builder will have enough n free space in inner-slice to write. The capacity extending only occurs when inner-slice’s capacity doesn’t have enough free space to write n bytes, e.g.</p><ul><li>Builder’s inner slice’s capacity: 10</li><li>Builder’s inner slice’s length: 5</li><li>If we call Grow(3) =&gt; the capacity isn’t extended because the current capacity’s frees pace is 5 bytes, it is enough to handle next 3 bytes.</li><li>If we call Grow(7) =&gt; the capacity is extended because capacity’s free space’s only 5 bytes and can’t handle next 7 bytes.</li></ul><p>A quick quiz with this case, if we call Grow(7), what value is the final capacity when it’s extended?</p><pre>17 or 12?</pre><p>Actually, it is extended to27.The Grow() method of strings.Builder increase the inner-slice’s capacity to value current_capacity * 2 + n (with n is a number of bytes that you want to extend). That’s why the extended capacity will be 10*2+7 = 27.</p><p>Another note when you assume about the capacity of strings.Builder when pre-defining. The rune and a character of string can be more than 1 bytes when you WriteRune() or WriteString(), if you know, the <a href="https://golang.org/pkg/unicode/utf8/#pkg-constants">UTF-8</a></p><h3>4. String()</h3><p>As bytes.Buffer, strings.Builder supports String() method to get final result string. For saving memory allocation, it converts the inner-buffer bytes to string as result with pointer technique. So the String() save space and time for converting.</p><pre>*(*string)(unsafe.Pointer(&amp;bytes))</pre><h3>5. Do not copy</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a4IwPDq3tEJJ_FRZfhreyQ.png" /></figure><p>The strings.Builder doesn’t recommend to copy to use. If you try to copy the strings.Builder and try to Write to it, you will got a panic</p><pre>var b1 strings.Builder<br>b1.WriteString(&quot;ABC&quot;)<br>b2 := b1<br>b2.WriteString(&quot;DEF&quot;) <br>// illegal use of non-zero Builder copied by value</pre><p>As you know, the strings.Builder bases on the internal slice to storing and manage their content. The slice, internally, consists of a pointer to the array where real data is storing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*KD02pGfasisf8I_BWE_JKQ.png" /><figcaption>Slice internals</figcaption></figure><p>When we copy the Builder, we clone the pointer of the slice but they still point to the old array. The problem will be occurs when you try to Write something to copied Builder or source Builder, the other’s content will be affects. That’s reason why strings.Builder prevent copy actions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*Ppak_h63S_TvYzJa2sFCpA.png" /></figure><p>Just an exception with zero content builder that doesn’t Write anything yet. We can copy the zero content without any error.</p><pre>var b1 strings.Builder<br>b2 := b1<br>b2.WriteString(&quot;DEF&quot;)<br>b1.WriteString(&quot;ABC&quot;)</pre><pre>// b1 = ABC, b2 = DEF</pre><p>The strings.Builder checks the copy action on following methods:</p><pre>Grow(n int)<br>Write(p []byte)<br>WriteRune(r rune)<br>WriteString(s string)</pre><p>So, it’s fine if we copy and use methods:</p><pre>// Reset()<br>// Len()<br>// String()</pre><pre>var b1 strings.Builder<br>b1.WriteString(&quot;ABC&quot;)<br>b2 := b1<br>fmt.Println(b2.Len())    // 3<br>fmt.Println(b2.String()) // ABC<br>b2.Reset()<br>b2.WriteString(&quot;DEF&quot;)<br>fmt.Println(b2.String()) // DEF</pre><h3>6. Concurrency supporting</h3><p>As bytes.Buffer, the strings.Builder doesn’t support concurrency when writing and reading. So we should take care it if we need them.</p><p>We can try a little bit with strings.Builder to add 10000 character, at the same time.</p><pre>package main</pre><pre>import (<br> &quot;fmt&quot;<br> &quot;strings&quot;<br> &quot;sync&quot;<br>)</pre><pre>func main() {<br> var b strings.Builder<br> var wait sync.WaitGroup<br> for i := 0; i &lt; 10000; i++ {<br>  wait.Add(1)<br>  go func() {<br>   b.WriteString(&quot;1&quot;)<br>   wait.Done()<br>  }()<br> }<br> wait.Wait()<br> fmt.Println(len(b.String()))<br>}</pre><p>If you run it, you had different result’s lengths. But they aren’t enough 10000 as we add.</p><pre>go run main.go =&gt; 7329<br>go run main.go =&gt; 7650<br>go run main.go =&gt; 7623</pre><h3>7. io.Writer interface</h3><p>The io.Writer interface is implemented on strings.Builder with Write() method Write(p []byte) (n int, err error). So, we have a lot of useful case with io.Writer:</p><ul><li>io.Copy(dst Writer, src Reader) (written int64, err error)</li><li>bufio.NewWriter(w io.Writer) *Writer</li><li>fmt.Fprint(w <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#Writer">Writer</a>, a …interface{}) (n <a href="https://golang.org/pkg/builtin/#int">int</a>, err <a href="https://golang.org/pkg/builtin/#error">error</a>)</li><li>func (r *http.<a href="https://golang.org/pkg/net/http/#Request">Request</a>) Write(w <a href="https://golang.org/pkg/io/">io</a>.<a href="https://golang.org/pkg/io/#Writer">Writer</a>) <a href="https://golang.org/pkg/builtin/#error">error</a></li><li>and other libraries that uses io.Writer</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*MhBcQBYT4ocfA7ftVT2iGw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=65260daae6e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What’s changes in Golang 1.10]]></title>
            <link>https://medium.com/@thuc/whats-changes-in-golang-1-10-d3022fbade3c?source=rss-6b6df8950fc3------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3022fbade3c</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[release-notes]]></category>
            <category><![CDATA[new-version]]></category>
            <dc:creator><![CDATA[Thuc Le]]></dc:creator>
            <pubDate>Sun, 18 Feb 2018 15:05:24 GMT</pubDate>
            <atom:updated>2018-02-18T15:05:24.637Z</atom:updated>
            <content:encoded><![CDATA[<p>Happy lunar new year and happy Golang 1.10 releasing. I took a quick look release note and notes interesting changes in Golang 1.10. Hope it’s useful for you.</p><h4>[Language] Support shift operation</h4><p>In go1.9 or above, the following shifting code throws an error:</p><pre><em>list := []int{0,1}<br>s</em> := uint(0)<br>list[1.0&lt;&lt;s] //invalid operation: 1 &lt;&lt; s (shift of type float64)</pre><pre>// Go 1.9 and above</pre><p>The shifting method you see is a non-constant shifting, left operand 1.0 is float64 and make the statement is the error because of shifting of type float64. But with some changes of code, you can see the language isn’t consistency:</p><pre>list := []int{0, 1}<br>list[1.0&lt;&lt;0] //return value is 1</pre><pre>// Go 1.9 and above</pre><p>or the compiler doesn’t accept shift operator above but it accepts the following code:</p><pre>s := uint(2)<br>_ = make([]int, 1.0&lt;&lt;s) //Don&#39;t through error</pre><pre>// Go 1.9 and above</pre><p>Actually, the compiler should check if the expression is a non-constant shift and left operand is untyped constant, it is converted to the type that would assume if the left operand stays alone. <a href="https://golang.org/ref/spec#Operators">https://golang.org/ref/spec#Operators</a></p><p>In the example list[1.0&lt;&lt;s] above, the non-constant shift expression 1.0&lt;&lt;s has left operand 1.0 is untyped constant, so the compiler will convert 1.0 to the type that is assumed in list[1.0]. It means 1.0 is converted to 1 before do the shift operator and the code work.</p><pre>list := []int{0,1}<br>s := uint(0)<br>list[1.0&lt;&lt;s] //Value is 1</pre><pre>//Go 1.10</pre><p>To understand the “converting to type that would assume if the left operand of shift operation stay alone”, we try to take a look an example</p><pre>s := uint(2)<br>var example1 int = 1.0 &lt;&lt; s <br>var example2 float64 = 1.0 &lt;&lt; s</pre><p>In example1, the 1.0 is converted to int. The operation 1.0 &lt;&lt; s works.<br>In example2, the 1.0 is converted to float64. The operation 1.0 &lt;&lt; s through error because shifting method doesn’t work with float64 in left operand</p><blockquote>Constant shifts: the shift operation has left and right operand are constants<br>Non-constant shifts: the shift operation has left or right operand aren’t constants</blockquote><p>Changes detail: <a href="https://go-review.googlesource.com/c/go/+/81277">https://go-review.googlesource.com/c/go/+/81277</a></p><h4>[Language] Update spec document about method expressions</h4><p>The document of language spec defines the method expressions:</p><pre>MethodExpr = ReceiverType &quot;.&quot; MethodName .<br>ReceiverType = TypeName | &quot;(&quot; &quot;*&quot; TypeName &quot;)&quot; | &quot;(&quot; ReceiverType &quot;)&quot; .</pre><p>But, long time ago, the Golang accepts the receiver type more than TypeName. The receiver type is any type. Two example that you familiar with are embedded struct type and interface. They don’t type name of the method but it takes the role of receiver type.</p><pre>type TypeName struct {}<br>func (a TypeName) String() {<br>   fmt.Print(a)<br>}</pre><pre>type AnotherTypeName struct{<br>   TypeName<br>}</pre><pre>type InterfaceName interface{<br>   String()<br>}</pre><pre>// Usage<br>AnotherTypeName{TypeName{}}.String()<br>var i InterfaceName = TypeName{}<br>i.String()</pre><p>Changes detail: <a href="https://github.com/golang/go/commit/f2d52519e1fad35566afb46ef521934cf0f5e5fd">https://github.com/golang/go/commit/f2d52519e1fad35566afb46ef521934cf0f5e5fd</a></p><h4>Ports changes</h4><ul><li>Go 1.9 doesn’t support <a href="https://www.freebsd.org/releases/9.3R/announce.html">FreeBSD 9.3</a> anymore. Require <a href="https://www.freebsd.org/releases/10.3R/announce.html">FreeBSD 10.3</a></li><li>Go 1.10 supports again on NetBSD and require <a href="https://www.netbsd.org/changes/changes-8.0.html">unreleased version 8</a></li><li>Go 1.10 is last release run on OS X 10.8. The next Go 1.11 will require OS X 10.10 or later</li><li>Go 1.10 is last release run on Window XP or Window Vister. The next Go 1.11 will require Window 7 or later</li></ul><h4>[Tool] Default GOROOT</h4><p>If the environment $GOROOT isn’t set, the go tool will use the default GOROOT during compilation.</p><h4>[Tool] GOTMPDIR</h4><p>By default, the go tool creates temporary files and directories in the system temporary path, such as $TMPDIR in Unix system. The new changes in Go1.10 allow we create the temporary files and directories in $GOTMPDIR that we define ourself.</p><h4>[Tool] Build and install</h4><p>In go 1.9 or lower, the building step detects changes of packages base on the modification times. It decides to re-build with new changes or use the caching if the package is unchanged.</p><p>Now, in Go 1.10, the changes purely base on the source code file and specified build flags/metadata. So go build check the changes instead of modification time to know packages are out of date. We also don’t need to rungo build -a to force it to build in case the modification time’s wrong in some case. If the new source code of packages is different, they will be re-build.</p><p>Another change with building step in Go 1.10 is options -asmflags, -gcflags, -gccgoflags, and -ldflags, now they only apply by default to packages that’s listed on the command line when build/install/or get. Example with go build -ldflags=-abc github.com/cweill/gotests, the flag -abc is only applied to build the github.com/cweill/gotests,its dependencies aren’t applied, such as io/ioutil or regexp</p><p>Another new thing with -asmflags, -gcflags, -gccgoflags, and -ldflags, they support pattern -asmflags=pattern=flags (the same with others) that only apply to packages matching the pattern. <br>For example, -ldflags=cmd/gofmt=-X=main.version=1.2.3 cmd/... only apply tag -X option to linker flags for cmd/gofmt</p><p>go install command now installs only packages and commands listed directly on the command line. <br>For example: go install cmd/gofmt install the gofmt program but not any packages on which it depends.<br>To force installation of dependencies, we use new flag -i: go install -i</p><h4>[Tool] Go test</h4><p>The same with build and installation, the go test command supports test result caching. If the test and command line match to previous successful run, go test will use caching result with note “cached”.</p><p>go test command now runs the go vet on the testing package to identify significant problems before running the test. To disable the running of go vet on go test, use flag -vet=off: go test -vet=off</p><p>When the test is failed by timeout, it write their profiles before exiting</p><p>New flag go test -failfast disables running additional tests after any test fails (except parallel testing)</p><p>And new flag go test -json is supports go tool test2json that convert the test output to JSON format that’s suitable for automated processing</p><h4>[Tool] Go doc</h4><p>Now, go doc tool supports to display function that returns slice of T or *T when displaying type T. For example with go doc mail.Address</p><p>Go 1.9:</p><pre>package mail // import &quot;net/mail&quot;</pre><pre>type Address struct {<br> Name    string // Proper name; may be empty.<br> Address string // user@domain<br>}<br>    Address represents a single mail address. An address such as &quot;Barry Gibbs<br>    &lt;<a href="/service/mailto:bg@example.com">bg@example.com</a>&gt;&quot; is represented as Address{Name: &quot;Barry Gibbs&quot;, Address:<br>    &quot;<a href="/service/mailto:bg@example.com">bg@example.com</a>&quot;}.</pre><pre>func ParseAddress(address string) (*Address, error)<br>func (a *Address) String() string</pre><p>Go 1.10:</p><pre>package mail // import &quot;net/mail&quot;</pre><pre>type Address struct {<br> Name    string // Proper name; may be empty.<br> Address string // user@domain<br>}<br>    Address represents a single mail address. An address such as &quot;Barry Gibbs<br>    &lt;<a href="/service/mailto:bg@example.com">bg@example.com</a>&gt;&quot; is represented as Address{Name: &quot;Barry Gibbs&quot;, Address:<br>    &quot;<a href="/service/mailto:bg@example.com">bg@example.com</a>&quot;}.</pre><pre>func ParseAddress(address string) (*Address, error)<br><strong><em>func ParseAddressList(list string) ([]*Address, error)</em></strong><br>func (a *Address) String() string</pre><h4>[Tool] Go fix</h4><p>In go 1.10, go fix command replaces golang.org/x/net/context to context in the import section.</p><h4>[Tool] Go get</h4><p>In go 1.10, go get support <a href="https://www.fossil-scm.org/">Fossil source code reopistories</a></p><h4>[Tool] Go vet</h4><p>The go vet command now always has access to complete, up-to-date type information when checking packages, even for packages using cgo or vendored imports.</p><h4>[Tool] Go fmt</h4><p>In go 1.10, go fmt update the formating of spacing in three-index:</p><pre>x[i+1 : j:k]     // Go 1.9<br>x[i+1 : j : k]   // Go 1.10</pre><p>New feature in go fmt supports the inline interface with single function. For example with a source code</p><pre>type Money interface { Fly() }</pre><p>With go fmt in version 1.9, the result is:</p><pre>type Money interface {<br>    Fly()<br>}</pre><p>But in version 1.10, the result is:</p><pre>type Money interface { Fly() }</pre><h4>Runtime</h4><p>The LockOSThread and UnlockOSThread() are functions that wires/unwires the calling goroutine to its current operating system thread. The calling goroutine will always execute in that thread, and no other goroutine will execute in it. The problem is with more than one LockOSThread, just one UnlockOSThread can unlock the thread. So it’s hard to detect how many LockOSThread is called to know when should call UnlockOSThread, especially library usage.</p><p>So in Go 1.10, we have a change when calling nest: if LockOSThread is called multiple times, UnlockOSThread must be called the same number of times in order to unlock the thread. Detail the changes you can follow links:<br><a href="https://github.com/golang/go/issues/20458">https://github.com/golang/go/issues/20458</a><br><a href="https://go-review.googlesource.com/c/go/+/45752">https://go-review.googlesource.com/c/go/+/45752</a></p><p>Now, There is no longer a limit on the <a href="https://tip.golang.org/pkg/runtime/#GOMAXPROCS">GOMAXPROCS</a> setting. In the older version, the limit is 1024. Unfortunate, I don’t have environment to test limit is greater 1024 :(</p><h4>[Library] bytes</h4><p>The Fields, FieldsFunc, Split, and SplitAfter functions of package bytesare always return the subslices of their slice input. It means the value of input and output is shared in the same memory space. The subslice uses the capacity of input as their capacity. So when we append to a subslice, it affects to next subslice. For example:</p><pre>package main</pre><pre>import (<br> &quot;bytes&quot;<br> &quot;fmt&quot;<br>)</pre><pre>func main() {<br> abc := []byte(&quot;Hello World&quot;)<br> xyz := bytes.Fields(abc)</pre><pre>fmt.Printf(&quot;*** Before change\n&quot;)<br> fmt.Printf(&quot;first: %q\n&quot;, xyz[0])<br> fmt.Printf(&quot;second: %q\n&quot;, xyz[1])</pre><pre>xyz[0] = append(xyz[0], byte(&#39;X&#39;))<br> xyz[0] = append(xyz[0], byte(&#39;Y&#39;))<br> xyz[0] = append(xyz[0], byte(&#39;Z&#39;))</pre><pre>fmt.Printf(&quot;\n*** After change\n&quot;)<br> fmt.Printf(&quot;first: %q\n&quot;, xyz[0])<br> fmt.Printf(&quot;second: %q\n&quot;, xyz[1])<br>}</pre><p>In Go 1.9, we will got the following output</p><pre>*** Before change<br>first: &quot;Hello&quot;<br>second: &quot;World&quot;</pre><pre>*** After change<br>first: &quot;HelloXYZ&quot;<br>second: &quot;YZrld&quot;</pre><p>In Go 1.10, the Fields, FieldsFunc, Split, and SplitAfter will return subslices with correct capacity, so when we append to a subslice, the data is cloned to another memory space and doesn’t affect to next one.</p><pre>*** Before change<br>first: &quot;Hello&quot;<br>second: &quot;World&quot;</pre><pre>*** After change<br>first: &quot;HelloXYZ&quot;<br>second: &quot;World&quot;</pre><h4>[Library] net/url</h4><p>The method ResolveReference() now is updated to not rewrite multiple slashes to a single slash, that makes the URL result incorrectly. For example:</p><pre>base, _ := url.Parse(&quot;http://host//path//to/page1&quot;)<br>target, _ := url.Parse(&quot;page2&quot;)<br>fmt.Println(base.ResolveReference(target))</pre><p>In go 1.9, the resolved URL is</p><pre>http://host/path//to/page2</pre><p>In go 1.10, the resolved URL is</p><pre>http://host//path//to/page2</pre><p>The change will make some case that URL isn’t the same before. So need to check the source code again after upgrading.</p><p>Another change in net/url, the UserInfo pointer’s methods: Password(), String(), Username(). If the pointer is nil, methods will return zero value instead panic as old go version.</p><pre>package main</pre><pre>import (<br> &quot;net/url&quot;<br> &quot;fmt&quot;<br>)</pre><pre>func main() {<br> var test * url.Userinfo<br> password,isSet :=test.Password()<br> fmt.Println(&quot;Result of test.Password():&quot;, password, isSet)<br> fmt.Println(&quot;Result of test.String():&quot;, test.String())<br> fmt.Println(&quot;Result of test.Username():&quot;, test.Username())<br>}</pre><pre>/*</pre><pre>Result of test.Password():  false<br>Result of test.String():<br>Result of test.Username():<br>*/</pre><h4><strong>[Library] strings</strong></h4><p>Golang 1.10 persent a new type strings.Builder that is a replacement for bytes.Buffer to build accumulating text into a string result</p><pre>package main</pre><pre>import (<br> &quot;fmt&quot;<br> &quot;strings&quot;<br>)</pre><pre>func main() {<br> var b strings.Builder<br> for i := 3; i &gt;= 1; i-- {<br>  fmt.Fprintf(&amp;b, &quot;%d...&quot;, i)<br> }<br> b.WriteString(&quot;ignition&quot;)<br> fmt.Println(b.String())</pre><pre>}</pre><p>The output’s</p><pre>3...2...1...ignition</pre><p>If I have time, I will take another note that talk detail about new API string builder :)</p><h4>[Library] bufio</h4><p>Golang 1.10 add new methods: Size() in bufio.Reader and bufio.Writer to report the underlying buffer size</p><h4>[Library] crypto/tls</h4><p>The TLS server now supports for SHA-512 when using TLS 1.2</p><h4>[Library] unicode</h4><p>Golang 1.10, the unicode package and associated support throughout the system has been upgraded from Unicode 9.0 to Unicode 10.0, which adds 8,518 new characters, including four new scripts, one new property, a Bitcoin currency symbol, and 56 new emoji.</p><h4><strong>Another things:</strong></h4><p>We have a new document: <a href="https://tip.golang.org/doc/diagnostics.html">https://tip.golang.org/doc/diagnostics.html</a></p><p>I just note some changes that I’m interesting with and I hope it’s helpful for you also. Other changes maybe make you more interesting, and you can take detail at <a href="https://golang.org/doc/go1.10">https://golang.org/doc/go1.10</a>.</p><p>Btw, Limina, the company I’m working, is looking for new position Golang Backend and Technical Manager remote work in Vietnam. If you are interested in that job or think you fit with the company, you can apply at <a href="/service/mailto:andreas@limina.com">andreas@limina.com</a>. You also take a look us at <a href="http://limina.com/">http://limina.com</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3022fbade3c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What’s new in vim-go 1.12]]></title>
            <link>https://medium.com/@thuc/whats-new-in-vim-go-1-12-920ac63888d7?source=rss-6b6df8950fc3------2</link>
            <guid isPermaLink="false">https://medium.com/p/920ac63888d7</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[vim]]></category>
            <dc:creator><![CDATA[Thuc Le]]></dc:creator>
            <pubDate>Thu, 30 Mar 2017 03:42:52 GMT</pubDate>
            <atom:updated>2017-03-30T03:42:52.062Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*A3g60UtkqaLy77EQNdtJag.png" /></figure><p>Vim-go have been released new version 1.12. So, we take a look what’s changes.</p><p>Before going to move to new changes in new version 1.12, vim-go’s installed/updated through <a href="https://github.com/fatih/vim-go#install">Pathogen, vim-plug, NeoBundle, Vundle, and Vim packages</a></p><h4><strong><em>:GoAddTags</em></strong> and <strong><em>:GoRemoveTags</em></strong></h4><p>There are two new commands that used to add tags into pre-defined structs automatically. You can check the following example how to use them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PuHJVYD_A1rWzAky15KeQw.gif" /></figure><p>Currently, there support syntax:</p><pre>:GoAddTags<br>:GoAddTags [key,[option]] [key,[option]] [key,[option]]</pre><pre>:GoRemoveTags<br>:GoRemoveTags [key,[option]] [key,[option]] [key,[option]]</pre><ul><li>If you don’t give any key when GoAddTags, the default’s json<em>.</em></li><li>The options only add to existed keys. If the key’s not exist, it’s no effect.</li><li>Each command only add/remove a option to a key. If you want add/remove more options to a key, type many times:</li></ul><pre>:GoAddTags json,opmitempty<br>:GoAddTags json,custom</pre><pre>:GoRemoveTags json,opmitempty<br>:GoRemoveTags json,custom</pre><ul><li>When remove key with a option, it will remove the option instead the key.</li><li>If you don’t define key when GoRemoveTags , It removes all of keys.</li></ul><h4>errl snippet</h4><p>Vim-go 1.12 support new snippet to add common error handler. In order to use the snippet function, make sure you install <a href="https://github.com/Shougo/neosnippet.vim">neosnippet</a> or <a href="https://github.com/SirVer/ultisnips">ultisnips</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*56r6JudlQ2E6YiC5CLzkEQ.gif" /></figure><p>The new commands used to change build tags for tools such as guru, gorename, etc …</p><p><strong>Other updates</strong></p><ul><li>The new commands, :GoBuildTags, used to change build tags for tools such as guru, gorename, etc …</li><li>vim-go works now even if GOPATH is not set (starting with Go 1.8). It because <a href="https://beta.golang.org/doc/go1.8#gopath">Go 1.8 now has default GOPATH</a></li><li>Improve docs for :GoDef , godoc view for vertical splits</li><li>Imporve highlight trimming syntax for Go templates</li><li>Fixing bugs…</li></ul><p>I updated big changes in vim-go 1.12 above. For more detail, you can double check the source and change logs at: <a href="https://github.com/fatih/vim-go/releases/tag/v1.12">https://github.com/fatih/vim-go/releases/tag/v1.12</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=920ac63888d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SHAttered attack — quick look]]></title>
            <link>https://medium.com/@thuc/shattered-attack-quick-look-3dc216533bbf?source=rss-6b6df8950fc3------2</link>
            <guid isPermaLink="false">https://medium.com/p/3dc216533bbf</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[shattered-attack]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[shattered]]></category>
            <category><![CDATA[sha-1]]></category>
            <dc:creator><![CDATA[Thuc Le]]></dc:creator>
            <pubDate>Mon, 27 Feb 2017 06:07:31 GMT</pubDate>
            <atom:updated>2017-02-27T06:07:31.494Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zaa2-TmOOnm1sCoDSt2cnA.jpeg" /></figure><h3>SHAttered attack</h3><p>Today, I have seen a lot of articles and news about “SHA-1 is broken in practice”. It makes me curious and I decide to take a little time to check: what’s it.</p><p>Simple explain: You have 2 different contents/documents but their hashes are the same.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/556/1*vL9MyEMFl24MlgCENDkspQ.png" /></figure><p>Is it dangerous? Let thinking about you download a file from internet. you hash and compare it with the prepared hashed string in web. They are the same. But are you sure the downloaded file is the same with provided from internet?</p><p>And I think a lot of dangerous cases you will meet that told in following link</p><p><a href="https://shattered.io/">SHAttered</a></p><p>But do you have question how it work? How are different contents have the same hashed SHA-1 string? First, take a look about SHA-1 work.</p><h3>SHA-1 hash function</h3><blockquote>SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function designed by NIST. SHA-1 produces a 160-bit (20-byte) hash value known as a message digest. — from <a href="https://en.wikipedia.org/wiki/SHA-1">Wikipedia</a></blockquote><p>More detail about SHA-1 hash function, let me explain in simple words:</p><p><strong>Step1</strong>: SHA-1 hash function convert input content to 8-bit binary numbers. In case the original content “<strong>thuc”</strong>, the 8-bit binary numbers are:</p><blockquote>thuc = t h u c = 01110100 01101000 01110101 01100011</blockquote><p><strong>Step2</strong>: it add a bit <strong><em>1</em></strong> at the end of binaries of step 1. The bit<em> </em><strong><em>1</em></strong> works as the separator between data and padding (will be added in next steps).</p><blockquote>01110100 01101000 01110101 01100011 <strong>1</strong></blockquote><p><strong>Step3</strong>: the function adds zeros to the end until the length congruent to 448 modulo 512. That means when we get the result’s length of step 3 divide by 512, the remainder must be 448. The length from step 3 is:</p><blockquote>32 (original content) + 1 (byte 1 at step 2) = 33</blockquote><p><strong>Step4</strong>: the function adds original content length (in binary) with 64 bits representation to the end. So, the length’s an exact multiple of 512 (448 + 64). In case the original content <strong>thuc</strong>, the length in binary that used to add is:</p><blockquote>Decimal representation: 8bit*4char = 32</blockquote><blockquote>64 bits representation: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 01000000</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*ADsk1fXhqWniLk7WRCbIlQ.png" /><figcaption>Step 1–4</figcaption></figure><p><strong>Step5</strong>: The data is broken (n) of 512: M1, M2, M3, M4, M5, M6… The result of <strong>step4</strong> is always multiple of 512, so each block’s length of M is also 512.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*tLcC9wVDxNe4E-Eu_8Mrqg.png" /></figure><p><strong>Step6</strong>: The hash function create predefined initial value (<a href="https://en.wikipedia.org/wiki/Initialization_vector">IV</a>) as the chaining value with 160 bits. It uses the IV and first broken data M1 (step5) as parameters of compression internal function and output the V1. The V1 is used with M2 as parameter of the same compression function and output the V2. The final V(n) ( compress of V(n-1) and M(n) ) is the hashed string value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/1*LyAhzhAqYVKUGwsZWoYWWQ.jpeg" /></figure><h3>SHAttered attack</h3><p>You have understood the overall of SHA-1 hash function. So, I will let you know about overall of SHAttered attack work that is based on <a href="https://en.wikipedia.org/wiki/Collision_attack#Chosen-prefix_collision_attack">chosen-prefix collision attack</a>.</p><p>First, let think about you want change a block content at M2, what’s happen with hash function. When we change the data in block M2 to M2&#39;, the output of compression between M2&#39; and V1 is different with V2, so we call it V2&#39;. Next, the V2&#39; will be used to create to V3&#39; and next’s V4&#39;. Finally, the result of SHA-1 of original input and changed input are different.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*W9hK1p8oGdQDhUuTsd0ucg.png" /></figure><p>Normally, different inputs return different output when using hash (mostly). But it alway have exception. So remember, when we return a smaller output from any length input, we are able to received duplicated cases. Maybe it’s very small, but it’s always have.</p><p>From shattered.io’s report, they shows the real cases that do that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/948/1*V36NYu3t_ywXE5skmFqHfQ.png" /></figure><p>You can see in table above, the CV0 is the same. The M1 is changed, so CV1 are different. But they change the M2 also, that make the CV2 are the same again. So, what’s mean? It means they changed the content but the result of hashing or next CV are don’t change. And Who check 2 contents with SHA-1 will think they are the same content.</p><p>In real data, the change maybe is middle part of content:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*47Vf-9XyHy5zzA9ss0WIPQ.png" /></figure><p>As diagram above, the M2 change to M2&#39;, the compression output the CV1&#39; instead of CV1. The guy also change the M3&#39; that make sure the output of next compression is V3.</p><p>The most difficult task is finding the correct M-start (M2) and M-end (M3) that satisfy the chosen prefix criteria PP and that cancel themselves nicely afterwards. The probabilities are small, so they try to call the SHA-1 compression function a lot of times to find them (M-start and M-end). As report from google, they need around 6⁶⁴ SHA-1 compression call.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eS4HH8U1KMcqWJnerSjvNg.png" /></figure><p>You got overall of SHAttered attack, actually, I skipped steps about compression function and how you to search near-collision blocks. They are hard to describe as simple words, so I suggestion you take a view at following report</p><iframe src="https://drive.google.com/viewerng/viewer?url=https%3A//shattered.io/static/shattered.pdf&amp;embedded=true" width="600" height="780" frameborder="0" scrolling="no"><a href="https://medium.com/media/18a7217eff4123f6f7c7b319b1cb93c5/href">https://medium.com/media/18a7217eff4123f6f7c7b319b1cb93c5/href</a></iframe><h3>End</h3><p>I hope my posting is useful for you to take the overall of SHAttered attack. And you will have many alternative choices when choosing hash functions.</p><p>I also share some materials that maybe useful for you also:</p><p><strong>Publish website</strong>:</p><p><a href="https://shattered.io/">SHAttered</a></p><p><strong>Infographic</strong>:</p><iframe src="https://drive.google.com/viewerng/viewer?url=https%3A//shattered.io/static/infographic.pdf&amp;embedded=true" width="600" height="780" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f7da19f12c8b4d0c150e8baaa6c32b6/href">https://medium.com/media/4f7da19f12c8b4d0c150e8baaa6c32b6/href</a></iframe><p><strong>Papper</strong>:</p><iframe src="https://drive.google.com/viewerng/viewer?url=https%3A//shattered.io/static/shattered.pdf&amp;embedded=true" width="600" height="780" frameborder="0" scrolling="no"><a href="https://medium.com/media/18a7217eff4123f6f7c7b319b1cb93c5/href">https://medium.com/media/18a7217eff4123f6f7c7b319b1cb93c5/href</a></iframe><p><strong>New Collision Attacks on SHA-1 Based on Optimal Joint Local-Collision Analysis:</strong></p><p><a href="https://link.springer.com/chapter/10.1007%2F978-3-642-38348-9_15">New Collision Attacks on SHA-1 Based on Optimal Joint Local-Collision Analysis - Springer</a></p><p><strong>SHA1</strong>:</p><p><a href="https://en.wikipedia.org/wiki/SHA-1">SHA-1 - Wikipedia</a></p><p><strong>Collision attack</strong>:</p><p><a href="https://en.wikipedia.org/wiki/Collision_attack">Collision attack - Wikipedia</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3dc216533bbf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Out of memory with Append in Golang]]></title>
            <link>https://medium.com/silicon-straits/out-of-memory-with-append-in-golang-956e7eb2c70e?source=rss-6b6df8950fc3------2</link>
            <guid isPermaLink="false">https://medium.com/p/956e7eb2c70e</guid>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[memories]]></category>
            <dc:creator><![CDATA[Thuc Le]]></dc:creator>
            <pubDate>Wed, 01 Jun 2016 01:15:27 GMT</pubDate>
            <atom:updated>2016-06-04T15:16:10.766Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QrP3uWcGAZfNWFDCwZOdmQ.jpeg" /></figure><p>Just a quick note for an issue that you might get with Out of Memory in Golang.</p><p>As you know, slice in Golang is powerful and easy to use. With build-in func in Golang, it helps us resolving many problem in programming.</p><p>But, today, I want to discuss more about slice and its memory.</p><p>First thing, the easiest way to create slice is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0c1c66cb112b6820e0942e477c87110/href">https://medium.com/media/b0c1c66cb112b6820e0942e477c87110/href</a></iframe><p>When you create a slice, 3 important information will be defined. It consists of a pointer to the array, the length of the segment, and its capacity (the maximum length of the segment).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*ijYaG2dX-dCZ7JuKKKCnoA.png" /></figure><blockquote>The length is the number of elements referred to by the slice</blockquote><blockquote>The capacity is the number of elements in the underlying array (beginning at the element referred to by the slice pointer)</blockquote><p>So, when you create slices with the way I posted above, the length and capacity will be minimum.</p><p>We move to the next section, the <em>append</em> function in Golang.</p><p><a href="https://golang.org/pkg/builtin/#append">https://golang.org/pkg/builtin/#append</a></p><blockquote>The append built-in function appends elements to the end of a slice. If it has sufficient capacity, the destination is resliced to accommodate the new elements. If it does not, a new underlying array will be allocated. Append returns the updated slice.</blockquote><p>The problem that’s we frequently think the <em>append</em> will append new elements to the end of a slice and forget about <strong>if we don’t have enough capacity, it will allocate a new slice with different memory space</strong>.</p><p>And with the simplest ways to create the slice, the capacity always fit with the current element in slice. And unlucky, we see source code that puts function <em>append</em> to a loop or nested loop. Everything’s fine with quick loop, but with bigger loop, the <em>append</em> is nightmare, it’s will eat the memory hungrily.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44ae851c6881518a6095d2c2a1bce123/href">https://medium.com/media/44ae851c6881518a6095d2c2a1bce123/href</a></iframe><p>You can imagine, each loop row will allocate a new memory with higher capacity and it’s not release yet. And we have:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/319da5a146e7842667bc2f8a983e3562/href">https://medium.com/media/319da5a146e7842667bc2f8a983e3562/href</a></iframe><h3>Out of memory</h3><p>The built-in Golang also supports us to pre-define the capacity, that make the <em>append</em> doesn’t allocate a new memory because of lack of capacity.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/464a75d1d91e4fb3ab3417b832771eda/href">https://medium.com/media/464a75d1d91e4fb3ab3417b832771eda/href</a></iframe><p>So, main idea when I write this post that is I hope we should think more about how memory is manipulated while coding.</p><p>And hope this will be helpful for you :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=956e7eb2c70e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/silicon-straits/out-of-memory-with-append-in-golang-956e7eb2c70e">Out of memory with Append in Golang</a> was originally published in <a href="https://medium.com/silicon-straits">Silicon Straits</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Connect Arduino Nano with Mac OSX]]></title>
            <link>https://medium.com/@thuc/connect-arduino-nano-with-mac-osx-f922a46c0a5d?source=rss-6b6df8950fc3------2</link>
            <guid isPermaLink="false">https://medium.com/p/f922a46c0a5d</guid>
            <category><![CDATA[nano]]></category>
            <category><![CDATA[macos]]></category>
            <category><![CDATA[arduino]]></category>
            <dc:creator><![CDATA[Thuc Le]]></dc:creator>
            <pubDate>Sat, 20 Feb 2016 12:45:47 GMT</pubDate>
            <atom:updated>2017-06-27T01:20:46.661Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>The guide is written with Mac OSX El Capitan, Arduino Nano V3 ( built by China ), Arduino IDE 1.6.5. Hope so it will be work with your situation.</blockquote><p>The problem that I got is that the Arduino IDE in OSX El Capitan can’t upload the code to my Arduino Nano V3. In Arduino IDE, don’t have any port that is USB.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*S5pRBi6xo4qkZEbh2bhnLg.jpeg" /><figcaption>Arduino V3</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2P-6-AakjOt85-EJzRMzoQ.png" /><figcaption>Don’t have port to choose ( only have bluetooth in MacOS )</figcaption></figure><p>A little sad, but I try to check way to resolve this problem… Maybe it’s from the Driver of my OS that doesn’t support USB port of Arduino Nano.</p><blockquote>My MacOS and Arduino IDE still work with Arduino Uno R3</blockquote><p>When I checked the driver of Mac OS to work with Arduino Nano, I saw a link, you can download it from <a href="https://github.com/ledongthuc/example/blob/master/file/CH34x_Install_V1.3.zip">this</a>. After download it, install it through<strong> CH34x_Install.pkg</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BiTVJD4DZ2YbC3IOAsd4Vw.png" /><figcaption>Install file through CH34X_Install.pkg</figcaption></figure><p>When installing was successful, open the terminal, run the command:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3730f11518fbfb03941944287ed20dbd/href">https://medium.com/media/3730f11518fbfb03941944287ed20dbd/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q6YkNbG7aqETLmGzidHw0Q.png" /><figcaption>Run the script</figcaption></figure><p>Then, restart the Mac. Check the files in /dev/ with start name “tty*”, if you see <strong>/dev/tty.wch ch341 USB=&gt;RS232 1420</strong>, it is correct.</p><p>Now, try to open the Arduino IDE, you will see the port <strong>/dev/tty.wch ch341 USB=&gt;RS232 1420</strong> in list</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*veenNobvIExU6HTs2B_FMw.png" /></figure><p>But when you try to upload the code to the board from Arduino IDE, you will get the error:</p><blockquote>avrdude: ser_open(): can’t open device “/dev/cu.wch”: No such file or directory<br>ioctl(“TIOCMGET”): Inappropriate ioctl for device<br>Problem uploading to board. See <a href="http://www.arduino.cc/en/Guide/Troubleshooting#upload">http://www.arduino.cc/en/Guide/Troubleshooting#upload</a> for suggestions.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VLdxQqJ-5UEAy2en8ClgpQ.png" /></figure><p>The reason is the name of serial port that used in Arduino is not support the space, ya. So, we need a trick with Arduino IDE. We need to replace the text -P{serial.port} to “-P{serial.port}” in 2 files:</p><ul><li>/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/platform.txt</li><li>/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/programmers.txt</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*RrAQ97wKs5bixAJGLm4RWA.png" /><figcaption>An example of replacement</figcaption></figure><blockquote>. We need replace all cases in 2 files.<br>. Assume your Arduino IDE install in path /Applications/Arduino.app</blockquote><p>Close and try to upload with Arduino IDE again:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*ju1tTGKBdxIXEaHIGf_j0A.png" /><figcaption>Deploy successfully</figcaption></figure><p>And we are done :) . So, hope it’s useful for you and leave me your comment if any.</p><p>Summarize steps:</p><blockquote>1. Download the driver from url: <a href="https://github.com/ledongthuc/example/blob/master/file/CH34x_Install_V1.3.zip">link</a></blockquote><blockquote>2. Extract and install file <strong>CH34x_Install.pkg</strong></blockquote><blockquote>3. Run the command: <strong>sudo nvram boot-args=&quot;kext-dev-mode=1&quot;</strong></blockquote><blockquote>4. Restart the Mac.</blockquote><blockquote>5. Replace the text <strong>-P{serial.port}</strong> to <strong>&quot;-P{serial.port}&quot;</strong> in 2 files:<br> + /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/platform.txt<br> + /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/programmers.txt</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f922a46c0a5d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Merry Christmas with electric reindeer]]></title>
            <link>https://medium.com/@thuc/merry-christmas-with-electric-reindeer-4dbc375740ed?source=rss-6b6df8950fc3------2</link>
            <guid isPermaLink="false">https://medium.com/p/4dbc375740ed</guid>
            <category><![CDATA[christmas]]></category>
            <category><![CDATA[arduino]]></category>
            <category><![CDATA[iot]]></category>
            <dc:creator><![CDATA[Thuc Le]]></dc:creator>
            <pubDate>Mon, 28 Dec 2015 01:54:46 GMT</pubDate>
            <atom:updated>2016-02-14T18:22:25.333Z</atom:updated>
            <content:encoded><![CDATA[<p>I wanted to make something special in Christmas this year. I decided to make a reindeer, no an electric reindeer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*o21NgpgHK4lhgwVUFMg-1g.jpeg" /><figcaption>Electric Reindeer</figcaption></figure><h3>Skeletons</h3><p>You need an reindeer’s skeleton before decorating anything. In this post, we will use mica silicate to make the skeleton.</p><p><strong>Step 1:</strong> The first step you need that is download the design file of skeleton. Unless you want to design yourself :)</p><p>PDF file: <a href="https://dl.dropboxusercontent.com/u/90901929/Reindeer/Reindeer.pdf">https://dl.dropboxusercontent.com/u/90901929/Reindeer/Reindeer.pdf</a></p><p>Adobe illustrator file: <a href="https://dl.dropboxusercontent.com/u/90901929/Reindeer/Reindeer.ai">https://dl.dropboxusercontent.com/u/90901929/Reindeer/Reindeer.ai</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2YTfPETfoelmH_zHPIYK_A.png" /><figcaption>Design of skeleton</figcaption></figure><p><strong>Step 2:</strong> Send the design file (PDF or AI) to someone who has the mica laser cutter machine. They will help you cut it.</p><blockquote>Some notes with laser cutter machine:</blockquote><blockquote>+ Mica should be black or brown. Your reindeer will be cooler</blockquote><blockquote>+ Size of design is 80 x 85 cm</blockquote><blockquote>+ The thickness of the mica should be 4mm or 3.5mm (at least 3mm)</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*ToEeCLusG598OnSfEhYhTQ.jpeg" /><figcaption>Laser cutter machine</figcaption></figure><p>When it finished, you will have the parts of skeleton</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vu2oRQkZ5VESfUH97FcjXQ.jpeg" /><figcaption>Mica skeleton</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6VJMWeJRbo8mg8tQTGQDlA.jpeg" /><figcaption>Mica skeleton</figcaption></figure><blockquote>In case you don’t know how to seek the mica laser machine, you can replace it by wood or paper carton. Print the design into A4 paper and cut it with wood or paper carton. In the end of post, I add some images of paper carton version, you can take a look.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*qfp3cslY8Sg0NQbRRJWR1Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*_-yIfJ1463xgrOJytW73qw.jpeg" /><figcaption>Paper carton or wood</figcaption></figure><p><strong>Step 3: </strong>Put the parts all together.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5kySQCyThzny-wunBDRLzQ.jpeg" /><figcaption>Put parts of head (don’t use number 7)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cGrsTMI-57ZWtE-WGO1_-Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uZsw4jXIFLvGlvH_jD59ig.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GMkO7MrU8vvqaos0ykR7Dw.jpeg" /><figcaption>We will have a head of reindeer</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*ojYhIMP_R4jy8O0JfYz3_Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y8XQ5NMEC8McP8QlrF7HyQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zpourE8jFKYWaQelsj4Cyg.jpeg" /><figcaption>The base (small)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_SVh1LwqVf_A8BLO1G0Wlw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zMPEG_4yQS3_IjjxLKFQ1w.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n9Actsx1FcQWPqU6pDj3UA.jpeg" /><figcaption>The base (large)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o3vdUF72Lp-s7N21D9JJQA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uzUF7v6DHqZNZqUFfBjVWw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4mlDuetvLSdL3aW1CdAvxg.jpeg" /><figcaption>Deerhorn</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mJVi0LcbFcjbTgSuLmWgiA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bQvSNCqrXrq2MHrO44kHjg.jpeg" /><figcaption>Deerhorn</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CJGlhbQZ49lzcE1I4hKqDg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A84eU-CUcQIPbgFa1xDlTw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oT2SKCR4m6-o5Df_sRVkUw.jpeg" /><figcaption>Done skeleton</figcaption></figure><h3>Blink reindeer’s eyes</h3><p>we need an microcontroller or microprocessor to blink the reindeer’s eyes. We will use Arduino Uno board to do it, it’s easy for guide (you can choose an others that you familiar with).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*zeAkekTK5O2cMEnVYx_1fw.jpeg" /><figcaption>Arduino Uno</figcaption></figure><p><strong>Step 1:</strong> We will prepare the circuit to blink the reindeer’s eyes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*oSJ3mV0W22FpNqElO1yMwQ.png" /><figcaption>Link leds with arduino</figcaption></figure><p>Now, we run following code with Arduino, it will blink the leds.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6db5f50d483895cf882b4d0d51eeee55/href">https://medium.com/media/6db5f50d483895cf882b4d0d51eeee55/href</a></iframe><p><strong>Step 2:</strong> We will paste the leds in to reindeer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VTm17sfIdH_sP2CRr1yZ8w.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WlRxFgJlIB4O-5Wu1p5-MQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kxqKL1TxDnI8Zk9MXOp8rw.jpeg" /><figcaption>Paste the leds into reindeer</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZUukxU6oaccCwRmUwOQtKg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l3db1hqPUa4B9mI1XezyGQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hfC3KntmUo8XzVUegheEtw.jpeg" /><figcaption>Paste arduino into reindeer</figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FS7Lj9-MerAA%3Ffeature%3Doembed&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DS7Lj9-MerAA%26feature%3Dyoutu.be&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FS7Lj9-MerAA%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/937549abd605ea761f99c9b00adcf77c/href">https://medium.com/media/937549abd605ea761f99c9b00adcf77c/href</a></iframe><p>Other code to blink reindeer’s eyes, change it if you want</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72be6482390a46af93bc334a98bf7cb6/href">https://medium.com/media/72be6482390a46af93bc334a98bf7cb6/href</a></iframe><p>P/s: As I mention above, I capture some pics of paper version.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*XNsseO0YPDbBjp7FG1Sg-w.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iq8vM4n_IQmyiQcKLSNyUg.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4dbc375740ed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Quick look with EMBD - Golang Embedded]]></title>
            <link>https://medium.com/@thuc/quick-look-with-embd-golang-embedded-135330449abd?source=rss-6b6df8950fc3------2</link>
            <guid isPermaLink="false">https://medium.com/p/135330449abd</guid>
            <dc:creator><![CDATA[Thuc Le]]></dc:creator>
            <pubDate>Thu, 17 Dec 2015 01:12:51 GMT</pubDate>
            <atom:updated>2015-12-17T01:12:51.447Z</atom:updated>
            <content:encoded><![CDATA[<p>When developing embedded application, I’ve frequently used C, C++ or Python as a main programming language. C and C++ have good performance but they take me a lot of time to prototype and develop an application with complex logic. Beside that, we need build again or change a lot to codes when I move the application to other hardware platform. Otherwise, Python is more flexible and easier for developing application, it’s cross platform language also. But the performance isn’t its strength, as you know. So, I’m looking for some language can adapt both, speed and easy developing.</p><p>Recently, I had a change to use the GO, an programming language is developed at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. It is a statically typed language (I love it) with syntax loosely derived from that of C, adding garbage collection, type safety, additional built-in types and a large standard library. The GO provides a syntax and environment to build an application fast and fun as scripting language. Beside that, a compiled language makes GO is so fast with high performance. Sound great, huh? It has everything I need. So, I tried to think about developing embedded development in GO language. Took a short time to check, I saw <a href="http://embd.kidoman.io/">EMBD</a>.</p><p>EMBD is a library that provides a <em>hardware abstraction layer</em> for doing embedded programming in GO language. EMBD support platforms like the <a href="https://www.raspberrypi.org/"><em>Raspberry Pi</em></a> and <a href="http://beagleboard.org/BLACK"><em>BeagleBone Black</em></a>. Moreover, the application can work with all supported platforms without any changes.</p><p>Ya, in this post, I will show you how to use EMBD in Golang to implement a simple application, blinking the buildin led in Raspberry PI. Let do it:</p><h3>Example 1: build-in led in Raspberry</h3><p><strong>Step 1</strong>: Install GO language (<a href="https://golang.org/">https://golang.org/)</a> and set GOPATH (<a href="https://golang.org/doc/code.html#GOPATH">https://golang.org/doc/code.html#GOPATH)</a>. I assume you have experience in GO language before.</p><p><strong>Step 2</strong>: Install the EMBD package with commandline:</p><pre>$ go get github.com/kidoman/embd</pre><p><strong>Step 3</strong>: Create an source code file <em>helloword_embd.go</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23522ce30359a69d1db5c644c4144cda/href">https://medium.com/media/23522ce30359a69d1db5c644c4144cda/href</a></iframe><p><strong>Step 4</strong>: Build source code with target arm. You will got a file<strong><em> helloworld_embd</em></strong>:</p><pre>$ export GOOS=linux<br>$ export GOARCH=arm<br>$ go build helloworld_embd.go</pre><p><strong>Step 5</strong>: Copy file <strong><em>helloworld_embd </em></strong>to raspberry.</p><p><strong>Step 6: </strong>Run the application with root permission:</p><pre>$ sudo ./helloworld_embd</pre><p>You will see the green led in Raspberry blink every sec.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fszh3Kto2DQA%3Ffeature%3Doembed&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dszh3Kto2DQA%26feature%3Dyoutu.be&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fszh3Kto2DQA%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/6700c455514b85cde748a47f8a11b0a8/href">https://medium.com/media/6700c455514b85cde748a47f8a11b0a8/href</a></iframe><blockquote><em>You need command </em><strong><em>sudo </em></strong><em>because</em><strong><em> </em></strong><em>EMBD need access some file of system to fully control the hardware.</em></blockquote><p><strong>Optional</strong>: A little changes, you can make app more cool.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0c816b47a78a4f3bd8f705f0e515248/href">https://medium.com/media/f0c816b47a78a4f3bd8f705f0e515248/href</a></iframe><p>Is it easy? Now, we move to next example.</p><h3>Example 2: Control digital input with diode led</h3><p>We will create an simple circuit in order to demo how to EMBD control the digital pins.</p><p><strong>Step 1</strong>: Create a circuit as image below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YKO9fGNjLmzOsbzpDg48dg.png" /></figure><p><strong>Step 2</strong>: Create and build the source code</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d130f71c70eb50071ba5d50e8ea6f738/href">https://medium.com/media/d130f71c70eb50071ba5d50e8ea6f738/href</a></iframe><p><strong>Step 3</strong>: Copy to Raspberry and run</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F8qtmQ1lvBDk%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D8qtmQ1lvBDk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F8qtmQ1lvBDk%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/86939f414f7076c2e0665e1ff0127213/href">https://medium.com/media/86939f414f7076c2e0665e1ff0127213/href</a></iframe><p>So, with 2 simple demo above, we can see how easy when developing raspberry PI with EMBD.</p><h3>More</h3><h4>PWM pin</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b25e468b07f48a2898c1427b7e27f143/href">https://medium.com/media/b25e468b07f48a2898c1427b7e27f143/href</a></iframe><h4>Analog pin</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/763f28d1c5804a764ef1f454ac30a75f/href">https://medium.com/media/763f28d1c5804a764ef1f454ac30a75f/href</a></iframe><p>But it’s not at all, EMBD supports many of familiar sensors, that we can easy to interact with.</p><h4>TMP006 Thermopile sensor</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*1UYzeAzjvcFqJeje083Q2w.jpeg" /></figure><h4>BMP085 Barometric pressure sensor</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*fLmhe61CYBBljFkp2HfGQw.jpeg" /></figure><h4>BMP180 Barometric pressure sensor</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*rEPC3HcUs9TgPqPIw_TOsw.jpeg" /></figure><h4>LSM303 Accelerometer and magnetometer</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/1*T73Hh7txV7Inso3MW_h04Q.jpeg" /></figure><h4>L3GD20 Gyroscope</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*aFShHtu57_0uyg3i0jryCw.jpeg" /></figure><h4><strong>US020</strong> Ultrasonic proximity sensor</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*qFCx3p1pUlcbejopo1D7Yw.jpeg" /></figure><h4><strong>BH1750FVI</strong> Luminosity sensor</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*AI8nTxk0hl5zvPsaftSDeg.jpeg" /></figure><h4>Membrane 3x4 Matrix Keypad</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*FR7_FdDI3aBpDwngmZuG3A.jpeg" /></figure><h4>PCA9685</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*iFVFjoeQHhGD2-4-bNqWrQ.jpeg" /></figure><h4>MCP4725</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*Sljuh6iU-5o_Na7NEXjtWA.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=135330449abd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>