Created
May 14, 2015 07:54
-
-
Save vikashvikram/11f80aa51afb490e3d68 to your computer and use it in GitHub Desktop.
Examples of how to use slices in Go programming language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"bytes" | |
) | |
func AddOneToEachElement(slice []int) { | |
for i := range slice { | |
slice[i]++ | |
} | |
} | |
func SubtractOneFromLength(slice []int) []int { | |
slice = slice[0:len(slice)-1] | |
return slice | |
} | |
type path []byte | |
type Gopher []int | |
func (p *path) TruncateAtFinalSlash() { | |
i := bytes.LastIndex(*p, []byte("/")) | |
if i >= 0 { | |
*p = (*p)[0:i] | |
} | |
} | |
func (p path) ToUpper() { | |
for i, b := range p { | |
if 'a' <= b && b <= 'z' { | |
p[i] = b + 'A' - 'a' | |
} | |
} | |
} | |
func Insert(slice []int, index, value int) []int { | |
// Grow the slice by one element. | |
slice = slice[0 : len(slice)+1] | |
// Use copy to move the upper part of the slice out of the way and open a hole. | |
copy(slice[index+1:], slice[index:]) | |
// Store the new value. | |
slice[index] = value | |
// Return the result. | |
return slice | |
} | |
func Extend(slice []int, element int) []int { | |
n := len(slice) | |
if n == cap(slice) { | |
// Slice is full; must grow. | |
// We double its size and add 1, so if the size is zero we still grow. | |
newSlice := make([]int, len(slice), 2*len(slice)+1) | |
copy(newSlice, slice) | |
slice = newSlice | |
} | |
slice = slice[0 : n+1] | |
slice[n] = element | |
return slice | |
} | |
// Append appends the items to the slice. | |
// First version: just loop calling Extend. | |
func Append(slice []int, items ...int) []int { | |
for _, item := range items { | |
slice = Extend(slice, item) | |
} | |
return slice | |
} | |
func main() { | |
var buffer [20]int | |
i := 0 | |
for i < 20 { | |
buffer[i] = i | |
i++ | |
} | |
fmt.Println("buffer array: ", buffer) | |
//slice does not create a new array. it just passed pointer to header and length of slice (for the underlying array) | |
slice := buffer[5:15] | |
fmt.Println("slice i.e. buffer[5:15]: ", slice) | |
slice2 := slice[5:8] | |
fmt.Println("slice2 = slice[5:8]: ", slice2) | |
//loose first and last elements | |
slice = slice[1:len(slice)-1] | |
fmt.Println("slice = slice[1:len(slice)-1]: ", slice) | |
AddOneToEachElement(slice2) | |
fmt.Println("Adding one to each element of slice2") | |
fmt.Println("slice2: ", slice2) | |
//the change will reflect in slice as well. | |
fmt.Println("change will get reflected in slice and buffer array as well") | |
fmt.Println("slice: ", slice) | |
fmt.Println("buffer array: ", buffer) | |
//arguments are copy | |
fmt.Println("Before: len(slice) =", len(slice)) | |
newSlice := SubtractOneFromLength(slice) | |
fmt.Println("After: len(slice) =", len(slice)) | |
fmt.Println("After: len(newSlice) =", len(newSlice)) | |
//capacities of buffer array and slices | |
fmt.Println("capacity buffer array: ", cap(buffer)) | |
fmt.Println("capacity slice: ", cap(slice)) | |
fmt.Println("capacity slice2: ", cap(slice2)) | |
fmt.Println("capacity newSlice: ", cap(newSlice)) | |
pathName := path("/usr/bin/tso") // Conversion from string to path. | |
pathName.TruncateAtFinalSlash() | |
fmt.Printf("%s\n", pathName) | |
pathName.ToUpper() | |
fmt.Printf("%s\n", pathName) | |
slice3 := make([]int, 10, 15) | |
fmt.Printf("slice3 len: %d, cap: %d\n", len(slice3), cap(slice3)) | |
gophers := make([]Gopher, 10) | |
fmt.Printf("gophers len: %d, cap: %d\n", len(gophers), cap(gophers)) | |
slice4 := make([]int, 10, 20) // Note capacity > length: room to add element. | |
for i := range slice4 { | |
slice4[i] = i | |
} | |
fmt.Println(slice4) | |
slice4 = Insert(slice4, 5, 99) | |
fmt.Println(slice4) | |
slice5 := make([]int, 0, 5) | |
for i := 0; i < 10; i++ { | |
slice5 = Extend(slice5, i) | |
fmt.Printf("len=%d cap=%d slice=%v\n", len(slice5), cap(slice5), slice5) | |
fmt.Println("address of 0th element:", &slice5[0]) | |
} | |
slice6 := []int{0, 1, 2, 3, 4} | |
fmt.Println(slice6) | |
slice6 = Append(slice6, 5, 6, 7, 8) | |
fmt.Println(slice6) | |
slice7 := []int{55, 66, 77} | |
slice6 = Append(slice6, slice7...) | |
fmt.Println(slice6) | |
//built-in append examples for slice | |
//builtin append can change the header of slice | |
// Create a couple of starter slices. | |
slice = []int{1, 2, 3} | |
slice2 = []int{55, 66, 77} | |
fmt.Println("Start slice: ", slice) | |
fmt.Println("Start slice2:", slice2) | |
// Add an item to a slice. | |
slice = append(slice, 4) | |
fmt.Println("Add one item:", slice) | |
// Add one slice to another. | |
slice = append(slice, slice2...) | |
fmt.Println("Add one slice:", slice) | |
// Make a copy of a slice (of int). | |
//a nil slice is functionally equivalent to a zero-length slice, even though it points to nothing. | |
//It has length zero and can be appended to, with allocation | |
slice3 = append([]int(nil), slice...) | |
fmt.Println("Copy a slice:", slice3) | |
// Copy a slice to the end of itself. | |
fmt.Println("Before append to self:", slice) | |
slice = append(slice, slice...) | |
fmt.Println("After append to self:", slice) | |
//Strings are just read-only slices of bytes with a bit of extra syntactic support from the language. | |
slash := "/usr/ken"[0] | |
fmt.Println(slash) | |
usr := "/usr/ken"[0:4] | |
//We can also take a normal slice of bytes and create a string from it with the simple conversion | |
//str := string(slice) | |
//and go in the reverse direction as well: | |
slice8 := []byte(usr) | |
fmt.Println(slice8) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment