<p dir="auto">I've been using Go for about 9 months and over that time I've come to like the explicitness of most of the language's semantics. But I feel that the language lacks some convenient ways to handle slices as you will find in most other languages. I feel that adding HOFs such as map/filter/reduce would make code less verbose when handling slices without compromising on explicitness or type-safety.</p>
<p dir="auto">The current idiom for doing a map operation is something like:</p>
<div class="highlight highlight-source-go notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="s0 := []int{1,2,3}
s1 := make([]int, 3)

for i := range s0 {
    s1[i] = s0[i] * 2
}"><pre class="notranslate"><span class="pl-s1">s0</span> <span class="pl-c1">:=</span> []<span class="pl-smi">int</span>{<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>}
<span class="pl-s1">s1</span> <span class="pl-c1">:=</span> <span class="pl-en">make</span>([]<span class="pl-smi">int</span>, <span class="pl-c1">3</span>)

<span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">:=</span> <span class="pl-k">range</span> <span class="pl-s1">s0</span> {
    <span class="pl-s1">s1</span>[<span class="pl-s1">i</span>] <span class="pl-c1">=</span> <span class="pl-s1">s0</span>[<span class="pl-s1">i</span>] <span class="pl-c1">*</span> <span class="pl-c1">2</span>
}</pre></div>
<p dir="auto">While this is a trivial case, and I could have just mutated <code class="notranslate">s0</code> in this case, it is clearly very imperative and verbose compared to what it could be.</p>
<p dir="auto">I propose an alternative:</p>
<div class="highlight highlight-source-go notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="s0 := []int{1,2,3}
s1 := s0.map(func(x int) int {
    return x * 2
})"><pre class="notranslate"><span class="pl-s1">s0</span> <span class="pl-c1">:=</span> []<span class="pl-smi">int</span>{<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>}
<span class="pl-s1">s1</span> <span class="pl-c1">:=</span> <span class="pl-s1">s0</span>.<span class="pl-en">map</span>(<span class="pl-k">func</span>(<span class="pl-s1">x</span> <span class="pl-smi">int</span>) <span class="pl-smi">int</span> {
    <span class="pl-k">return</span> <span class="pl-s1">x</span> <span class="pl-c1">*</span> <span class="pl-c1">2</span>
})</pre></div>
<p dir="auto">In this case the function passed to map has two properties of interest. The argument which would need to be the slice's Kind (type-checked at compile time), and the return type which could be any type. The one caveat is that the type of the resulting slice will need to be inferred from the type returned from the function.</p>
<p dir="auto">For example:</p>
<div class="highlight highlight-source-go notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="s0 := []int{1,2,3}
s1 := s0.map(func(x int) float32 {
    return float32(x) / 2
})"><pre class="notranslate"><span class="pl-s1">s0</span> <span class="pl-c1">:=</span> []<span class="pl-smi">int</span>{<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">3</span>}
<span class="pl-s1">s1</span> <span class="pl-c1">:=</span> <span class="pl-s1">s0</span>.<span class="pl-en">map</span>(<span class="pl-k">func</span>(<span class="pl-s1">x</span> <span class="pl-smi">int</span>) <span class="pl-smi">float32</span> {
    <span class="pl-k">return</span> <span class="pl-en">float32</span>(<span class="pl-s1">x</span>) <span class="pl-c1">/</span> <span class="pl-c1">2</span>
})</pre></div>
<p dir="auto">where <code class="notranslate">s1</code> would have type <code class="notranslate">[]float32</code>.</p>
<p dir="auto">I feel like this seemingly low-hanging fruit would be a step in the right direction in terms of making the language more enjoyable to use with its lack of generics.</p>
<pre class="notranslate">See <a href="http://play.golang.org/p/OeSXsazIDS" rel="nofollow">http://play.golang.org/p/OeSXsazIDS</a>

If you unmarshal &lt;Param2 int="1" /&gt;

into

type Param2 struct {
    Int *int `xml:"int,attr"`
}

Int will always be nil. As seen in the example, this works if Int is an element and not
an attribute.</pre>