Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mashbridge
Created December 24, 2012 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mashbridge/4368333 to your computer and use it in GitHub Desktop.
Save mashbridge/4368333 to your computer and use it in GitHub Desktop.
How to calculate the dimensionality of an array in Go
func ArrayDepth(i interface{}) int {
return arrayDepth(i, 0)
}
func arrayDepth(i interface{}, n int) int {
kind := reflect.ValueOf(i).Kind()
if kind == reflect.Array || kind == reflect.Slice {
n += 1
j := i.([]interface{})
return arrayDepth(j[0], n)
}
return n
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment