Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Maps
Regex
Strings
Go
Arrays
Initialise
Insert
Count
Retrieve by index
Remove by index
Slice
Iterate
Search
Sort
Use as stack
Use as FIFO
Compare
Maps
Strings
Java
Arrays
Regex
Strings
JavaScript
Arrays
Functions
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Regex
Strings
Go Arrays
Initialise
Fixed contents n/a var a = []int{1,2,3,4,5}
1 2 3 4 5
Fixed contents [alternative] n/a a := []int{1,2,3,4,5}
1 2 3 4 5
Insert
Prepend element 'e' var a=[]int{2,3,4}
var e=1
a=append([]int{e},a ...)
1 2 3 4
Append element 'e' var a=[]int{1,2,3}
var e=4;
a=append(a,e)
1 2 3 4
Insert element 'e' at index 'n' var a=[]int{1,2,3,4,5}
var e=666
var n=3
a=append(a[:n], append([]int{e}, a[n:] ...)...)
1 2 3 666 4 5
Prepend array 'b' var a=[]int{1,2,3,4,5}
var b=[]int{10,20,30}
a=append(b, a...);
10 20 30 1 2 3 4 5
Append array 'b' var a=[]int{1,2,3,4,5}
var b=[]int{10,20,30}
a=append(a,b...)
1 2 3 4 5 10 20 30
Insert array 'b' at index 'n' var a=[]int{1,2,3,4,5}
var b=[]int{10,20,30}
var n=3
a=append(a[:n], append(b, a[n:]...)...)
1 2 3 10 20 30 4 5
Concatenate array 'a' and array 'b' var a=[]int{1,2,3}
var b=[]int{4,5,6}
var c=append(a,b...)
1 2 3 4 5 6
Overwrite
Overwrite first element var a=[]int{1,2,3}
a[0]=666
666 2 3
Overwrite last element var a=[]int{1,2,3}
a[len(a)-1]=666
1 2 666
Overwrite element at index 'n' var a=[]int{1,2,3}
var n=1
a[n]=666
1 666 3
Overwrite all elements var a=[]int{1,2,3,4,5,6}
var b=[]int{7,8,9}
a=b
7 8 9
Count
Length of array var a=[]int{1,2,3}
var n=len(a)
3
Size of underlying storage var a=[100]int{1,2,3}
var n=cap(a)
100
Retrieve by index
Retrieve first element var a=[]int{1,2,3}
var e=a[0]
1
Retrieve last element var a=[]int{1,2,3}
var e=a[len(a)-1]
3
Retrieve element 'n' var a=[]int{1,2,3}
var n=2
var e=a[n]
3
Remove by index
Remove element at index 'n' var a=[]int{1,2,3,4,5}
var n=2
a=append(a[:n], a[n+1:]...)
1 2 4 5
Remove elements in index range 'm' to 'n' (inclusive) var a=[]int{1,2,3,4,5}
var m=1
var n=3
a=append(a[:m], a[n+1:]...)
1 5
Remove elements from index 'n' onwards (inclusive) var a=[]int{1,2,3,4,5}
var n=3
a=a[:n]
1 2 3
Slice
Slice from index 'm' to 'n' (inclusive) var a=[]int{1,2,3,4,5}
var m=1
var n=3
a=a[m:n+1]
2 3 4
Slice from index 'n' onwards (inclusive) var a=[]int{1,2,3,4,5}
var n=2
a=a[n:]
3 4 5
Slice up to index 'n' (inclusive) var a=[]int{1,2,3,4,5}
var n=3
a=a[:n+1]
1 2 3 4
Iterate
Iterate over array 'a' var a=[]int{1,2,3,4,5}
var b=[]int{}
for _,x := range a {
  b=append(b,x*2)
}
2 4 6 8 10
Search
Test element 'e' 'is in' array var a=[]int{1,2,3,4,5}
var e=3
var t = ( sort.SearchInts(a, e) != len(a) )
1
Find index of unique element 'e' var a=[]int{1,2,3,4,5}
var e=3
var n = sort.SearchInts(a, e)
2
Sort
Sort in ascending order var a=[]int{5,1,4,3,2}
sort.Ints(a)
1 2 3 4 5
Sort in ascending order [alternative] var a=[]int{5,1,4,3,2}
sort.Sort(sort.IntSlice(a))
1 2 3 4 5
Sort in descending order var a=[]int{5,1,4,3,2}
// "sort.Reverse" wraps existing interface
// replacing Less with an inverse Less
sort.Sort(sort.Reverse(sort.IntSlice(a)))
5 4 3 2 1
Sort in custom order // The sort.Interface methods:
// Len, Less and Swap
type InReverse []int
func (s InReverse) Len() int {
  return len(s)
}
func (s InReverse) Less(i, j int) bool {
  return s[i] > s[j]
}
func (s InReverse) Swap(i, j int) {
  s[i], s[j] = s[j], s[i]
}
var a=[]int{5,1,4,3,2}
sort.Sort(InReverse(a))
5 4 3 2 1
Reverse var a=[]int{1,2,3,4,5}
for i := len(a)/2-1; i >= 0; i-- {
  opp := len(a)-1-i
  a[i], a[opp] = a[opp], a[i]
}
5 4 3 2 1
Reverse [alternative] var a=[]int{1,2,3,4,5}
for left, right := 0, len(a)-1;
    left < right;
    left, right = left+1, right-1 {
  a[left], a[right] = a[right], a[left]
}
5 4 3 2 1
Use as a stack
Push element 'e' var a=[]int{1,2,3,4,5}
var e=666
a=append(a,e)
1 2 3 4 5 666
Pop next element var a=[]int{1,2,3,4,5}
a=a[:len(a)-1]
1 2 3 4
Retrieve next element var a=[]int{1,2,3,4,5}
var e=a[len(a)-1]
5
Use as a FIFO
Add element 'e' var a=[]int{1,2,3,4,5}
var e=666
a=append(a,e)
1 2 3 4 5 666
Remove next element var a=[]int{1,2,3,4,5}
a=a[1:]
2 3 4 5
Retrieve next element var a=[]int{1,2,3,4,5}
var e=a[0]
1
Compare
Test for equality (true) var a=[]int{1,2,3,4,5}
var b=[]int{1,2,3,4,5}
var t=reflect.DeepEqual(a,b)
1
Test for equality (false) var a=[]int{1,2,3,4,5}
var b=[]int{1,2,3,4,50}
var t=reflect.DeepEqual(a,b)
0