funcgenerateMatrix(n int)[][]int{
ret :=make([][]int, n)for index :=range ret {
ret[index]=make([]int, n)}
now, end :=1, n*n +1
left, right, up, down :=0,len(ret[0])-1,0,len(ret)-1for{for i := left; i <= right; i++{
ret[up][i]= now
now++}if now == end {break}for i := up +1; i <= down; i++{
ret[i][right]= now
now++}if now == end {break}for i := right -1; i >= left; i--{
ret[down][i]= now
now++}if now == end {break}for i := down -1; i >= up +1; i--{
ret[i][left]= now
now++}if now == end {break}
up, down = up +1, down -1
left, right = left +1, right -1}return ret
}