<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>array on Golang</title><link>https://golang.k5kc.com/tags/array/</link><description>Recent content in array on Golang</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Sun, 16 Aug 2020 00:14:00 +0530</lastBuildDate><atom:link href="https://golang.k5kc.com/tags/array/index.xml" rel="self" type="application/rss+xml"/><item><title>Go - for ... range</title><link>https://golang.k5kc.com/2020/08/16/5.range/</link><pubDate>Sun, 16 Aug 2020 00:14:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/5.range/</guid><description>Intro for ... range clause can be used to iterate 5 types of variables: array, slice, string, map and channel, and the following sheet gives a summary of the items of for ... range loops:
Type 1st Item 2nd Item Array index value Slice index value String index (rune) value (rune) Map key value Channel value Example - Iterating on slice package main import &amp;#34;fmt&amp;#34; func main() { s := [] {1,2,3} for i,v := range s { fmt.</description></item><item><title>Go - Array</title><link>https://golang.k5kc.com/2020/08/16/2.array/</link><pubDate>Sun, 16 Aug 2020 00:11:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/2.array/</guid><description>Syntax In Go, the length is also a part of array type. So the following code declares an array:
var array [3]int while &amp;ldquo;var slice []int&amp;rdquo; defines a slice. Arrays cannot be resized.
Another way is to set the array entries as you declare the array:
func main() { a := [2]string{&amp;#34;hello&amp;#34;, &amp;#34;world!&amp;#34;} fmt.Printf(&amp;#34;%q&amp;#34;, a) } You can also use an implicit length when passing values to array using [...]:</description></item><item><title>Adding multiple types to collection</title><link>https://golang.k5kc.com/2020/08/16/5.adding-multiple-types-to-collection/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/5.adding-multiple-types-to-collection/</guid><description>I would generally recommend avoiding this, but sometimes it could be neccessary. To add multiple types to a single map you must declare the map to take the interface type. The interface type is comparable to Object in Java. Everyhing is an interface.
package main import &amp;#34;fmt&amp;#34; func main(){ m := map[string]interface{}{ &amp;#34;test&amp;#34;:1, &amp;#34;test2&amp;#34;:&amp;#34;test&amp;#34;, } fmt.Println(m) s := []interface{}{ &amp;#34;test&amp;#34;, 2, } fmt.Println(s) }</description></item><item><title>Adding sugar - map, reduce and filter, etc to slice</title><link>https://golang.k5kc.com/2020/08/16/6.add-sugar-mapreducefilter-to-slice/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/6.add-sugar-mapreducefilter-to-slice/</guid><description>Golang prides itself on being a simple , pragmatic language and tried to avoid sugar that its creators feel are unnecessary. So lets add some map reduce functions ourselves to see what is involved:
package main import &amp;#34;fmt&amp;#34; type MyList []string // a custom type can be a builtin type if you want it to be func (ml MyList) Filter(f func(string) bool) []string { na := []string{} for _, v := range ml { if add := f(v); add { na = append(na, v) } } return na } func (ml MyList) Reduce(f func(prev, current string, index int) string) []string { na := []string{} for i, v := range ml { if i == 0 { na = append(na, f(&amp;#34;&amp;#34;, v, i)) } else { na = append(na, f(ml[i-1], v, i)) } } return na } func (ml MyList) Map(f func(val string) *string) []string { na := []string{} for _, v := range ml { mVal := f(v) if nil !</description></item><item><title>Conversion between array and slice</title><link>https://golang.k5kc.com/2020/08/16/conversion-between-array-and-slice/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/conversion-between-array-and-slice/</guid><description>In Go, array is a fixed length of continuous memory with specified type, while slice is just a reference which points to an underlying array. Since they are different types, they can&amp;rsquo;t assign value each other directly. See the following example:
package main import &amp;#34;fmt&amp;#34; func main() { s := []int{1, 2, 3} var a [3]int fmt.Println(copy(a, s)) } Because copy only accepts slice argument, we can use the [:] to create a slice from array.</description></item><item><title>Reallocating underlying array of slice</title><link>https://golang.k5kc.com/2020/08/16/reallocating-underlying-array-of-slice/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/reallocating-underlying-array-of-slice/</guid><description>When appending data into slice, if the underlying array of the slice doesn&amp;rsquo;t have enough space, a new array will be allocated. Then the elements in old array will be copied into this new memory, accompanied with adding new data behind. So when using Go built-in append function, you must always keep the idea that &amp;ldquo;the array may have been changed&amp;rdquo; in mind, and be very careful about it, otherwise, it may bite you!</description></item></channel></rss>