-2

I'm making a simple game of sudoku using slices of a 9x9 2d array. I'm still starting out with Golang and have some C++ experience. I keep getting the error message "cannot use Sudoku[0:9][0] (type [9]int) as type []int in assignment".

var row1 []int = Sudoku[0][0:9] This line correctly took the values of the first row of the 2d array and placed them into the row1 slice, but using var col1 []int = Sudoku[0:9][0] results in the error message above. What can I do? Thanks in advance!

For example,

package main

import "fmt"

func main() {
    var Sudoku [9][9]int
    fmt.Println(Sudoku)
    var row1 []int = Sudoku[0][0:9]
    fmt.Println(row1)
    var col1 []int = Sudoku[0:9][0]
    fmt.Println(col1)
}

Playground: https://play.golang.org/p/Jk6sqqXR5VE

10:6: cannot use Sudoku[0:9][0] (type [9]int) as type []int in assignment
peterSO
  • 158,998
  • 31
  • 281
  • 276
Zbadrawy
  • 13
  • 2
  • 2
    Arrays like [9]int and slices like []int arer different types in Go. Take the Tour of Go. – Volker Apr 25 '18 at 18:49
  • took the tour. What I don't understand is what alternative. I want a slice to contain the values of one column of the 2d array. How can I do this considering the above method failed. – Zbadrawy Apr 25 '18 at 19:51

2 Answers2

1

TLDR: In Go, array and slice are not exactly the same. You can't assign a slice variable to an array value.

Details:

  1. We start with Sudoku [9][9]int variable is an array.

  2. The valid line var row1 []int = Sudoku[0][0:9]:

  3. Sudoku[0] returns the element at index 0 of Sudoku, which is an array [9]int. Let's say we call this temporary result temp

  4. Then temp[0:9]returns a slice of elements between index 0 and 9 (exclusive) in temp. That's why you can assign that to a slice variable row1

  5. Now the trouble line var col1 []int = Sudoku[0:9][0]. Based on the naming, I guess your intention is to return the first column from Sudoku array ? Unfortunately, this is what happens:

  6. Sudoku[0:9] returns the slice of elements between index 0 and 9 (exclusive) of Sudoku. Because each element in Sudoku is an array, this temp value actually holds all the rows from Sudoku and of type [][9]int.

  7. Now temp[0]returns the first element of temp, which actually is just the first row of Sudoku and of type array [9]int. Thus, Go will complain when you try to assign it to a variable of type []int (a slice)

So if Sudoku is a matrix of

1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
...
9 9 9 9 9 9 9 9 9

Even if Go does not complain about typing, the returned value will be [1 1 1 1 1 1 1 1 1] and not [1 2 3 4 5 6 7 8 9]

lvtien
  • 11
  • 1
0

var col1 []int = Sudoku[0:9][0] gets you an array, not a slice. You could either declare as var col1 [9]int = Sudoku[0:9][0] (or better: col1 := Sudoku[0:9][0]), or if you really want a slice: var col1Slice []int = col1[:] after getting col1.

In general, things would be much easier if your sudoku structure were a 2D slice instead of 2D array, then you'd be dealing with only slices.

Working example of all of these: https://play.golang.org/p/LE8qwFSy1m_e

nojo
  • 1,065
  • 6
  • 12