<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>slice on Golang</title><link>https://golang.k5kc.com/tags/slice/</link><description>Recent content in slice 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/slice/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>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>"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>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>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>