Golang: Collection


In this post I write down my experience regarding data collection in Golang, basically collection  is a grouping of some variable number of data items (Examples of collections include lists, sets, multisets, trees and graphs.).

Golang Array Example:

package main

import "fmt"

var (
	a [3]string
)

func main() {

	a[0] = "Hi"
	a[1] = "little"
	a[2] = "Buddy"

	fmt.Println(a[0], a[1])
	fmt.Printf("%s %s %s \n", a[0], a[1], a[2])
	fmt.Println(a)
}

Go Output:

Hi little

Hi little Buddy

[Hi little Buddy]

Golang Multi-dimensional arrays by example:


package main

import "fmt"

func main() {
	var a [2][3]string
	for i := 0; i < 2; i++ {
		for j := 0; j < 3; j++ {
			a[i][j] = fmt.Sprintf("row %d - column %d", i+1, j+1)
		}
	}

	for i := 0; i < 2; i++ {
		for j := 0; j < 3; j++ {
			fmt.Println(a[i][j])
		}
	}

	fmt.Println("------------------------")

	fmt.Printf("%q", a)
}

Go Output:

row 1 – column 1

row 1 – column 2

row 1 – column 3

row 2 – column 1

row 2 – column 2

row 2 – column 3

————————

[[“row 1 – column 1” “row 1 – column 2” “row 1 – column 3”] [“row 2 – column 1” “row 2 – column 2” “row 2 – column 3”]]

Set the array entries  while declare the array


package main

import "fmt"

func main() {

	a := []string{"aaaaa", "bbbbb"}
	a = append(a, "ccccc")

	fmt.Println(a[0]) // Index 0
	fmt.Println(a[1]) // Index 0
	fmt.Println(a[2]) // Index 0

	fmt.Printf("%s\n", a) // Print as string
	fmt.Printf("%q", a)   // Print as array collection
}

Go Output:

aaaaa

bbbbb

ccccc

[aaaaa bbbbb ccccc]

[“aaaaa” “bbbbb” “ccccc”]

Common errors related array in Golang:

  • Assign value to the undefined / invalid array index

./test.go:14: invalid array index 3 (out of bounds for 3-element array)

Error: process exited with code 2.

  • Assign value to undefined variable

./test.go:13: undefined: a

Error: process exited with code 2.

  • Index out of range

Just trying to define the array to be like this:


var (
	a []string
)

And the result is like this:

panic: runtime error: index out of range

goroutine 1 [running]:

panic(0x45afa0, 0xc42000a1e0)

/usr/local/go/src/runtime/panic.go:500 +0x1a1

main.main()

/home/yusuf/go-learn2/src/github.com/yoesoff/test_ide/test.go:9 +0x3a

Error: process exited with code 2.

Slicing a slice


package main

import "fmt"

func main() {
	mySlice := []int{11, 12, 13, 14, 15, 16}

	fmt.Println(mySlice[0])   // 11
	fmt.Println(mySlice[0:3]) // [11, 12,13]
	fmt.Println(mySlice[0:2]) // [11, 12]

	// Having same output with above code
	fmt.Println(mySlice[0])  // 11
	fmt.Println(mySlice[:3]) // [11, 12,13]
	fmt.Println(mySlice[:2]) // [11, 12]

	fmt.Println(mySlice[0:]) // [11 12 13 14 15 16]
	fmt.Println(mySlice[1:]) // [12 13 14 15 16]
	fmt.Println(mySlice[2:]) // [13 14 15 16]
	fmt.Println(mySlice[3:]) //[14 15 16]

}

Go Output:

11

[11 12 13]

[11 12]

11

[11 12 13]

[11 12]

[11 12 13 14 15 16]

[12 13 14 15 16]

[13 14 15 16]

[14 15 16]

Success: process exited with code 0.

Another Slicing  slice example


 package main

import "fmt"

func main() {
	mySlice := []int{11, 12, 13, 14, 15, 16}

	fmt.Println(mySlice[0:6])
	fmt.Println(mySlice[0:5])
	fmt.Println(mySlice[0:4])
	fmt.Println(mySlice[0:3])
	fmt.Println(mySlice[0:2])
	fmt.Println(mySlice[0:1])

	fmt.Println(mySlice[5:6])
	fmt.Println(mySlice[4:6])
	fmt.Println(mySlice[3:6])
	fmt.Println(mySlice[2:6])
	fmt.Println(mySlice[1:6])
	fmt.Println(mySlice[0:6])
}

Go Output:

[11 12 13 14 15 16]

[11 12 13 14 15]

[11 12 13 14]

[11 12 13]

[11 12]

[11]

[16]

[15 16]

[14 15 16]

[13 14 15 16]

[12 13 14 15 16]

[11 12 13 14 15 16]

Appending to a slice


package main

import "fmt"

func main() {
	// Code below won't work
	//	cities := []string{}
	//	cities[0] = "Santa Monica"

	// This code is working well
	cities := []string{}
	cities = append(cities, "Santa Monica")
	fmt.Println(cities)
}

Go Output:

[Santa Monica]

Success: process exited with code 0.

Append more


//append more
cities = append(cities, "San Diego", "Mountain View", "Jakarta", "Bali", "Trenggalek")
fmt.Printf("%q", cities)

Go Output:

[“Santa Monica” “San Diego” “Mountain View” “Jakarta” “Bali” “Trenggalek”]

Success: process exited with code 0.

check the length of a slice by using len()

cities = append(cities, "San Diego", "Mountain View", "Jakarta", "Bali", "Trenggalek")
fmt.Printf("%d", len(cities))

Go Output:

5

Another  exciting Example about slice

Go slices, usage and internals
Effective Go – slices
Append function documentation
Slice tricks
Effective Go – slices
Effective Go – two-dimensional slices
Go by example – slices

Common errors related slice in Golang:

  • slice bounds out of range

panic: runtime error: slice bounds out of range

goroutine 1 [running]:

panic(0x47a8e0, 0xc42000a1f0)

/usr/local/go/src/runtime/panic.go:500 +0x1a1

main.main()

/home/yusuf/go-learn2/src/github.com/yoesoff/hello/hello.go:20 +0x53

Error above caused by this:

mySlice := []int{11, 12, 13, 14, 15, 16}

fmt.Println(mySlice[3:12]) // [14 15 16 undefined …]

  • slice bounds out of range

panic: runtime error: slice bounds out of range

goroutine 1 [running]:

panic(0x47a8e0, 0xc42000a1f0)

/usr/local/go/src/runtime/panic.go:500 +0x1a1

main.main()

/home/yusuf/go-learn2/src/github.com/yoesoff/hello/hello.go:8 +0x53

Error: process exited with code 2.

mySlice := []int{11, 12, 13, 14, 15, 16}

fmt.Println(mySlice[0:7000])  // max is 6

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s