gitee.com/quant1x/num@v0.3.2/shape.go (about)

     1  package num
     2  
     3  // Shape 返回一维或2维数组的行数和列数
     4  func Shape[T Number](x any) (r, c int) {
     5  	return __go_slice_shape[T](x)
     6  }
     7  
     8  func __go_slice_shape[T Number](x any) (r, c int) {
     9  	switch vs := x.(type) {
    10  	case T:
    11  		return 0, 0
    12  	case []T:
    13  		return len(vs), 0
    14  	case [][]T:
    15  		r = len(vs)
    16  		if r > 0 {
    17  			c = len(vs[0])
    18  		}
    19  		return
    20  	default:
    21  		return -1, -1
    22  	}
    23  
    24  }