Created
December 24, 2012 08:22
-
-
Save mashbridge/4368333 to your computer and use it in GitHub Desktop.
How to calculate the dimensionality of an array in Go
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
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