<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>collections on Golang</title><link>https://golang.k5kc.com/categories/collections/</link><description>Recent content in collections 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/categories/collections/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 - Maps</title><link>https://golang.k5kc.com/2020/08/16/4.maps/</link><pubDate>Sun, 16 Aug 2020 00:13:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/4.maps/</guid><description>Intro Maps are somewhat similar to what other languages call “dictionaries” or “hashes”. A map maps keys to values.
Here are some points to look at:
Maps allows us to quickly access to an element/value using a unique key. Map keys must be unique because otherwise it can&amp;rsquo;t find the corresponding values/elements. The types of Map Keys and Values in Maps can be different. A Map Key must be a comparable type.</description></item><item><title>Golang Slices</title><link>https://golang.k5kc.com/2020/08/16/3.slices/</link><pubDate>Sun, 16 Aug 2020 00:12:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/3.slices/</guid><description>Intro A slice in Golang is an abstraction that sits on top of an array. An array normally has a set size and must be initialised with a size. A slice is a little more like an array in javascript or an ArrayList in Java.
A slice is a descriptor of an array segment. It consists of a pointer to the array, the length of the segment, and its capacity (the maximum length of the segment).</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>"nil slice" vs "nil map"</title><link>https://golang.k5kc.com/2020/08/16/nil-slice-vs-nil-map/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/nil-slice-vs-nil-map/</guid><description>Slice and map are all reference types in Go, and their default values are nil:
package main import &amp;#34;fmt&amp;#34; func main() { var ( s []int m map[int]bool ) if s == nil { fmt.Println(&amp;#34;The value of s is nil&amp;#34;) } if m == nil { fmt.Println(&amp;#34;The value of m is nil&amp;#34;) } } The result is like this：
The value of s is nil The value of m is nil When a slice&amp;rsquo;s value is nil, you could also do operations on it, such as append:</description></item><item><title>Accessing Map</title><link>https://golang.k5kc.com/2020/08/16/accessing-map/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/accessing-map/</guid><description>Map is a reference type which points to a hash table, and you can use it to construct a &amp;ldquo;key-value&amp;rdquo; database which is very handy in practice programming. E.g., the following code will calculate the count of every element in a slice:
package main import ( &amp;#34;fmt&amp;#34; ) func main() { s := []int{1, 1, 2, 2, 3, 3, 3} m := make(map[int]int) for _, v := range s { m[v]++ } for key, value := range m { fmt.</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>Go - Slice copy</title><link>https://golang.k5kc.com/2020/08/16/array-copy/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/array-copy/</guid><description>The definition of built-in copy function is here:
func copy(dst, src []Type) int
The copy built-in function copies elements from a source slice into a destination slice. (As a special case, it also will copy bytes from a string to a slice of bytes.) The source and destination may overlap. Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst).
Let&amp;rsquo;s see a basic example in which source and destination slices aren&amp;rsquo;t overlapped:</description></item><item><title>Golang Collections Intro</title><link>https://golang.k5kc.com/2020/08/16/1.collections-intro/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/1.collections-intro/</guid><description>Golang has two main collection types.
maps.
arrays and slices</description></item><item><title>Pass slice as a function argument</title><link>https://golang.k5kc.com/2020/08/16/pass-slice-as-a-function-argument/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/pass-slice-as-a-function-argument/</guid><description>In Go, the function parameters are passed by value. With respect to use slice as a function argument, that means the function will get the copies of the slice: a pointer which points to the starting address of the underlying array, accompanied by the length and capacity of the slice. Oh boy! Since you know the address of the memory which is used to store the data, you can tweak the slice now.</description></item><item><title>Prepend in Slice</title><link>https://golang.k5kc.com/2020/08/16/prepend/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/prepend/</guid><description>Go has a built-in append function which add elements in the slice:
func append(slice []Type, elems &amp;hellip;Type) []Type
But how if we want to the &amp;ldquo;prepend&amp;rdquo; effect? Maybe we should use copy function. E.g.:
package main import &amp;#34;fmt&amp;#34; func main() { var s []int = []int{1, 2} fmt.Println(s) s1 := make([]int, len(s) + 1) s1[0] = 0 copy(s1[1:], s) s = s1 fmt.Println(s) } The result is like this:
[1 2] [0 1 2] But the above code looks ugly and cumbersome, so an elegant implementation maybe here:</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><item><title>Slice vs Arrays</title><link>https://golang.k5kc.com/2020/08/16/slice-vs-array/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/slice-vs-array/</guid><description>Performance Slice operations are cheap! Slicing: Creates a new slice header. Assigning a Slice to another Slice or, passing it to a function: Only copies the slice header. Slice header has a fixed size and it doesn't change even if we have got millions of elements. Array can be expensive as compared to Slice. Assigning an array to another array or passing it to a function: Copies all the elements of it.</description></item><item><title>The internals of slice</title><link>https://golang.k5kc.com/2020/08/16/the-internals-of-slice/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/the-internals-of-slice/</guid><description>There are 3 components of slice:
a) Pointer: Points to the start position of slice in the underlying array;
b) length (type is int): the number of the valid elements of the slice;
b) capacity (type is int): the total number of slots of the slice.
Check the following code:
package main import ( &amp;#34;fmt&amp;#34; &amp;#34;unsafe&amp;#34; ) func main() { var s1 []int fmt.Println(unsafe.Sizeof(s1)) } The result is 24 on my 64-bit system (The pointer and int both occupy 8 bytes).</description></item><item><title>Two-dimensional slice</title><link>https://golang.k5kc.com/2020/08/16/two-dimensional-slice/</link><pubDate>Sun, 16 Aug 2020 00:10:00 +0530</pubDate><guid>https://golang.k5kc.com/2020/08/16/two-dimensional-slice/</guid><description>Go supports multiple-dimensional slice, but I only want to introduce two-dimensional slice here. One reason is the two-dimensional slice is usually used in daily life, while multiple-dimensional seems not common. If you often use multiple-dimensional slice, personally I think the code is a little clumsy and not easy to maintain, so maybe you can try to check whether there is a better method; the other reason is the principle behind multiple-dimensional slice is the same with two-dimensional slice, you can also understand it if you know two-dimensional slice well.</description></item></channel></rss>