Talk is cheap.
func printMatrix(matrix [][]int) []int {
res := []int{}
if len(matrix) == 0 || len(matrix[0]) == 0 { return res }
x, y, i := 0, 0, 0
dx := []int{ 0, 1, 0, -1 }
dy := []int{ 1, 0, -1, 0 }
pos := []int{ 0, 0, len(matrix[0])-1, len(matrix)-1 }
for pos[0] <= pos[2] && pos[1] <= pos[3] {
for pos[0] <= y && y <= pos[2] && pos[1] <= x && x <= pos[3] {
res = append(res, matrix[x][y])
x, y = x+dx[i], y+dy[i]
}
x, y = x-dx[i], y-dy[i]
i = (i+1) % 4
pos[i] += dx[i]+dy[i]
x, y = x+dx[i], y+dy[i]
}
return res
}