GOLANG

The anatomy of Slices in Go

Slices are like Arrays but they can vary in length.

Uday Hiwarale
Sep 10, 2018 · 13 min read
(source: pexels.com)

What is a slice

var s []int
https://play.golang.org/p/JI6ikCK2f9x

slice is a reference to an array

https://play.golang.org/p/1naC_0qQz_E
https://play.golang.org/p/9xi8b8TTqHY

☛ Length and Capacity of a slice

https://play.golang.org/p/tKJaxdY7dYp
https://play.golang.org/p/eAbelmHUkZK

slice is a struct

type slice struct {
zerothElement *type
len int
cap int
}
https://play.golang.org/p/0jUjmjhTCos
https://play.golang.org/p/eEChIs0-66G

append function

func append(slice []Type, elems ...Type) []Type
https://play.golang.org/p/dSA5x7TkFeS
https://play.golang.org/p/qKtVAka498Z

anonymous array slice

https://play.golang.org/p/l_uhlR5KjNY
https://play.golang.org/p/dmcnLc6Ys8c

copy function

func copy(dst []Type, src []Type) int
https://play.golang.org/p/MkFRMZl-v1B

make function

func make(t Type, size ...IntegerType) Type
s := make([]type, len, cap)
https://play.golang.org/p/z0tlrRYLhMu

Type... unpack operator

https://play.golang.org/p/JfLgynyqVYc

[start:end] extract operator

https://play.golang.org/p/lNhNx5KGVrR

☛ Slice iteration

☛ Passed by reference

https://play.golang.org/p/p6O0Uqeww1g
[0 1 4 9 16 25 36 49 64 81]
https://play.golang.org/p/qE8grYQ8Q0s

☛ Delete slice element(s)

https://play.golang.org/p/LfLGN2m-uSm

slice comparison

https://play.golang.org/p/kZ7-SyCBvpt

Multi-dimensional slices

s1 := [][]int{
[]int{1, 2},
[]int{3, 4},
[]int{5, 6},
}
// type inference like arrays
s2 := [][]int{
{1, 2},
{3, 4},
{5, 6},
}

☛ Memory optimization

package mainimport "fmt"func getCountries() []string {
countries := []string{"United states", "United kingdom", "Austrilia", "India", "China", "Russia", "France", "Germany", "Spain"} // can be much more
return countries[:3]
}
func main() {
countries := getCountries()
fmt.Println(cap(countries)) // 9
}
package mainimport "fmt"func getCountries() (c []string) {
countries := []string{"United states", "United kingdom", "Austrilia", "India", "China", "Russia", "France", "Germany", "Spain"} // can be much more
c = make([]string, 3) // made empty of length and capacity 3
copy(c, countries[:3]) // copied to `c`
return
}
func main() {
countries := getCountries()
fmt.Println(cap(countries)) // 3
}

☛ Read more


RunGo

A place to find introductory Go Programming Language tutorials and learning resources. Like my other tutorials on Web Development, RunGo publication features important Go articles with deep dive into core of the language with examples and sample code.

Uday Hiwarale

Written by

IITI (2014) • Software Developer • India | Follow: http://github.com/thatisuday | Ask: thatisuday@gmail.com

RunGo

RunGo

A place to find introductory Go Programming Language tutorials and learning resources. Like my other tutorials on Web Development, RunGo publication features important Go articles with deep dive into core of the language with examples and sample code.

More From Medium

More from RunGo

More from RunGo

3.8K

More from RunGo

2.5K

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade