Skip to content

Instantly share code, notes, and snippets.

@anxiousmodernman
Last active August 29, 2015 14:21
Show Gist options
  • Save anxiousmodernman/119e56d18e8a3daad12c to your computer and use it in GitHub Desktop.
Save anxiousmodernman/119e56d18e8a3daad12c to your computer and use it in GitHub Desktop.
Slice of Slice
package main
import "fmt"
type IndentationStack []([]string) // type to represent a slice of slice-of-string
// can be written like [][]string, i think, but parens make it more clear
func main() {
var stack IndentationStack
stack = make([]([]string), 4) // initialize a slice of slices, length 4, inner slices have length 0!
fmt.Println("First it's all like", len(stack[0]))
dirA := "A"
dirB := "B"
dirC := "C"
stack.addToStack(dirA, 0) // add one string to the first slice at index 0
stack.addToStack(dirB, 0) // add another to the first slice at index 0
stack.addToStack(dirC, 1) // add one string to the second slice at index 1
fmt.Println("Stack is ", stack)
}
func (this IStack) addToStack(name string, level int) {
this[level] = append(this[level], name) // append name to one of the slices at index "level"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment