-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.xml
101 lines (96 loc) · 3.52 KB
/
feed.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>
<channel>
<atom:link href='http://goslices.com/' rel='self' type='application/rss+xml'/>
<generator>
clj-rss
</generator>
<title>
GoSlices
</title>
<link>
http://goslices.com/
</link>
<description>
Weekly slices of go knowledge in <200 words
</description>
<lastBuildDate>
Tue, 28 Apr 2015 18:51:41 +0100
</lastBuildDate>
<author>
</author>
<item>
<guid>
http://goslices.com/posts/2015-04-08-append.html
</guid>
<link>
http://goslices.com/posts/2015-04-08-append.html
</link>
<title>
Make and loop, don't append
</title>
<description>
<p>For a much better discussion of this topic read the <a href='http://blog.golang.org/slices'>go blog</a>.</p><p>Sometimes you might find yourself looping and appending to a list like this:<pre><code>func appendMapper&#40;ls &#91;&#93;int&#41; &#91;&#93;int {
result := &#91;&#93;int{}
for &#95;, x := range ls {
result = append&#40;result, x&#42;2&#41;
}
return result
}
</code></pre> It's tempting to do this. But append is one of those troublesome words that already means something in English:<pre><code>v. To add as a supplement or appendix
v. To fix to; attach
</code></pre> It sounds like what you want. but append has a very specific meaning in go. If the slice is as its capacity it increases the capacity above what is required before appending.<pre><code>package main
import &quot;fmt&quot;
func main&#40;&#41; {
ls := &#91;&#93;int{1}
fmt.Println&#40;len&#40;ls&#41;, cap&#40;ls&#41;&#41;
ls = append&#40;ls, 2&#41;
fmt.Println&#40;len&#40;ls&#41;, cap&#40;ls&#41;&#41;
ls = append&#40;ls, 3&#41;
fmt.Println&#40;len&#40;ls&#41;, cap&#40;ls&#41;&#41;
ls = append&#40;ls, 4, 5&#41;
fmt.Println&#40;len&#40;ls&#41;, cap&#40;ls&#41;&#41;
ls = append&#40;ls, 7, 8, 9, 10&#41;
fmt.Println&#40;len&#40;ls&#41;, cap&#40;ls&#41;&#41;
}
</code></pre> <a href='http://play.golang.org/p/kPcLAgGdPv'>Open in playground</a></p><p>Output:<pre><code>1 1
2 2
3 4
5 8
9 16
</code></pre> So by the end, even though we only have 9 items in the slice. 16 ints worth of bytes will have been allocated.</p><p>Sometimes this is unavoidable. But if you know the size of your output you should do this:<pre><code>func loopMapper&#40;ls &#91;&#93;int&#41; &#91;&#93;int {
result := make&#40;&#91;&#93;int, len&#40;ls&#41;&#41;
for i, x := range ls {
result&#91;i&#93; = x &#42; 2
}
return result
}
</code></pre> That way only the exact size needed is allocated.</p>
</description>
<pubDate>
Wed, 08 Apr 2015 00:00:00 +0100
</pubDate>
<author>
</author>
</item>
<item>
<guid>
http://goslices.com/posts/2015-04-02-first.html
</guid>
<link>
http://goslices.com/posts/2015-04-02-first.html
</link>
<title>
This blog
</title>
<description>
<p>This blog came off the back off doing the <a href='http://devcareerboost.com/blog-course/'>blog course</a> run by <a href='http://simpleprogrammer.com'>simple programmer</a>.</p>
</description>
<pubDate>
Thu, 02 Apr 2015 00:00:00 +0100
</pubDate>
<author>
</author>
</item>
</channel>
</rss>