<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Zero4J</title>
	<atom:link href="http://www.wuyb.com/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.wuyb.com/blog</link>
	<description>Joey's blog about science and technology</description>
	<lastBuildDate>Sun, 15 Nov 2009 07:20:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Go (0)</title>
		<link>http://www.wuyb.com/blog/?p=31</link>
		<comments>http://www.wuyb.com/blog/?p=31#comments</comments>
		<pubDate>Sun, 15 Nov 2009 06:09:40 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[go]]></category>

		<guid isPermaLink="false">http://www.wuyb.com/blog/?p=31</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><a href="http://golang.org">Go</a> is google&#8217;s new language. I am at the very first step learning it. It&#8217;s said Go is a &#8216;when Python meets C++&#8217; language, sadly I only know a little C++, nothing about Python. It has been a tough time for me to understand something inside. The first obstacle is the keyword &#8216;new&#8217;.</p>
<p>&#8216;new&#8217; in Go doesn&#8217;t have the same semantic as in C++. For value types, it allocates the  memory and returns the pointer to that piece of memory (in heap) &#8211; this is familiar, for example:</p>
<pre>type T struct {a, b int}
func main() {
    t := new(T);
    fmt.Printf("t.a=%d, t.b=%d\n", t.a, t.b);
}</pre>
<div>However, if you want to use something which should be constructed before use,  the keyword &#8216;new&#8217; doesn&#8217;t really work as expected. Try this:</div>
<pre>m := new(map[string]int);
m["one"] = 1;</pre>
<div>It seems nothing is wrong.  But actually if you run it,  &#8217;SIGSEGV: segmentation violation&#8217;. Why?</div>
<div>The reason is that &#8216;new&#8217; here only <strong>declares </strong>that m is a pointer to a map[string]int, and <strong>initialize</strong> m to an allocated piece of memory in heap. But that memory, is initialize to zero.  (in C++, the constructor is also invoked to really initialize the memory but Go type doesn&#8217;t have constructor/destructor).  So the pointer is ready, but memory is not.  Then, how should we (if really want) use new with map?</div>
<div>
<pre>m1 := new(map[string]int);
*m1 = make(map[string]int);
(*m1)["one"]=1;</pre>
<div>This is the answer. However, you can doesn&#8217;t mean you have to or should.  As it is a reference, there is no point to use a pointer anywhere &#8211; functions taking map as parameter makes no copy. If when necessary, &amp; will get the address of a map.</div>
<pre>m1 := make(map[string]int);
pm1 := &amp;m1;</pre>
<div>What&#8217;s behind these? To understand this, remember two things.</div>
<div>
<ol>
<li> Go doesn&#8217;t have constructor. So &#8216;new&#8217; will allocate memory, initialize it as zero. For those non-zero-value-is-useful type, explicit initialization is mandatory.</li>
<li> &#8216;make&#8217; is also used to allocate memory. But it applies only to map and channel which must be initialized before use, and array which is ready to be sliced.</li>
</ol>
</div>
<div>Why Go is designed like this? I don&#8217;t know&#8230;</div>
<div>Reference:</div>
<div>[1]<a href="http://golang.org/doc/go_tutorial.html"> Go Language Tutorial</a></div>
<div>[2]<a href="http://golang.org/doc/go_for_cpp_programmers.html"> Go for C++ Programmers</a></div>
<div>[3] <a href="http://golang.org/doc/effective_go.html#allocation_new">Effective Go</a></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.wuyb.com/blog/?feed=rss2&amp;p=31</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8216;Me&#8217; at Time Square</title>
		<link>http://www.wuyb.com/blog/?p=28</link>
		<comments>http://www.wuyb.com/blog/?p=28#comments</comments>
		<pubDate>Sat, 01 Aug 2009 01:40:42 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[topcoder]]></category>

		<guid isPermaLink="false">http://www.wuyb.com/blog/?p=28</guid>
		<description><![CDATA[
Story here. I am the second from left in the front row.
]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-29" title="Billboard" src="http://www.wuyb.com/blog/wp-content/uploads/2009/07/Billboard.jpg" alt="Billboard" width="474" height="592" /></p>
<p>Story <a href="http://www.topcoder.com/news/2009/07/22/tco09-champions-announced-in-times-square/">here</a>. I am the second from left in the front row.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wuyb.com/blog/?feed=rss2&amp;p=28</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>It&#8217;s final</title>
		<link>http://www.wuyb.com/blog/?p=25</link>
		<comments>http://www.wuyb.com/blog/?p=25#comments</comments>
		<pubDate>Tue, 14 Apr 2009 14:15:59 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[topcoder]]></category>

		<guid isPermaLink="false">http://www.wuyb.com/blog/?p=25</guid>
		<description><![CDATA[http://www.topcoder.com/news/2009/04/13/announcing-tco09-champions-assistant-and-saarixx/
*assistant is the 2009 TopCoder Open Component Development Champion!*
See his road to the top here.
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.topcoder.com/news/2009/04/13/announcing-tco09-champions-assistant-and-saarixx/"><em><span style="color: #0000ff;">http://www.topcoder.com/news/2009/04/13/announcing-tco09-champions-assistant-and-saarixx/</span></em></a></p>
<p><em><span style="color: #0000ff;">*<strong>assistant</strong> is the 2009 TopCoder Open Component Development Champion!*</span></em></p>
<p><em><span style="color: #0000ff;">See his road to the top </span></em><a href="http://www.topcoder.com/tco09?module=MemberResults&amp;eid=3009&amp;ct=471&amp;cr=20076717"><span style="color: #888888;"><em><span style="color: #0000ff;">here</span></em></span></a><em><span style="color: #0000ff;">.</span></em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wuyb.com/blog/?feed=rss2&amp;p=25</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
