0

I have this error :

# command-line-arguments
.\cheking.go:14: cannot use strconv.Itoa(i + 64) + strconv.Itoa(j + 48) (type st
ring) as type [8]int in assignment

code:

package main

import (
    "fmt"
    "strconv"
)

func main() {

    var board [8][8]int

    for i := 1; i <= 8; i++ { // initialize  array
        for j := 1; j <= 8; j++ {
            board[(j-1)+8*(i-1)] = (strconv.Itoa(i+64) + "" + strconv.Itoa(j+48)) // int to char

            fmt.Printf("%s \n", board[i][j])
        }
    }
}

2 Answers 2

3

strconv.Itoa is shorthand for FormatInt(int64(i), 10):

FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' for digit values >= 10.

so the result of strconv.Itoa(i+64) is string, and the board is not (this is the error).

I think you are trying to do something like this working sample code (let me know if not):

package main

import "fmt"

func main() {
    board := [8][8]string{}
    for i := 0; i < 8; i++ { // initialize  array
        for j := 0; j < 8; j++ {
            board[i][j] = string(i+65) + string(j+49) // int to char
            fmt.Printf("%s ", board[i][j])
        }
        fmt.Println()
    }
}

output:

A1 A2 A3 A4 A5 A6 A7 A8 
B1 B2 B3 B4 B5 B6 B7 B8 
C1 C2 C3 C4 C5 C6 C7 C8 
D1 D2 D3 D4 D5 D6 D7 D8 
E1 E2 E3 E4 E5 E6 E7 E8 
F1 F2 F3 F4 F5 F6 F7 F8 
G1 G2 G3 G4 G5 G6 G7 G8 
H1 H2 H3 H4 H5 H6 H7 H8

if my guess is fine, you may do it this way too:

package main

import "fmt"

func main() {
    board := [8][8]string{
        {"A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8"},
        {"B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8"},
        {"C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8"},
        {"D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8"},
        {"E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8"},
        {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8"},
        {"G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8"},
        {"H1", "H2", "H3", "H4", "H5", "H6", "H7", "H8"},
    }

    // print the board:
    for i := 0; i < 8; i++ {
        fmt.Println(board[i])
    }
}

output:

[A1 A2 A3 A4 A5 A6 A7 A8]
[B1 B2 B3 B4 B5 B6 B7 B8]
[C1 C2 C3 C4 C5 C6 C7 C8]
[D1 D2 D3 D4 D5 D6 D7 D8]
[E1 E2 E3 E4 E5 E6 E7 E8]
[F1 F2 F3 F4 F5 F6 F7 F8]
[G1 G2 G3 G4 G5 G6 G7 G8]
[H1 H2 H3 H4 H5 H6 H7 H8]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot ! That was what i needed.
0

First of all, if you want to initialize each board position with a string, you need to change the declaration of your board array:

var board [8][8]string

since strconv.Itoa returns a string.

Then, if you simply need to access each board location sequentially, you can simply update your inner loop:

// You don't need to iterate through the array like C using index arithmatic
for i := 0; i < 8; i++ { // initialize  array
        for j := 0; j < 8; j++ {
            // use whatever logic you are using to init each value
            board[i][j] = (strconv.Itoa(i+64) + "" + strconv.Itoa(j+48)) // int to char

            fmt.Printf("%s \n", board[i][j])
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.