使用Golang将int数组转换为char数组?
我有一个错误:使用Golang将int数组转换为char数组?,go,Go,我有一个错误: # 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 代码: package main import ( "fmt" "strconv" ) func main() { var board [8][8]int fo
# 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
代码:
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])
}
}
}
主程序包
进口(
“fmt”
“strconv”
)
func main(){
var板[8][8]内部
对于i:=1;istrconv.Itoa
是FormatInt(int64(i),10)
的缩写:
Formatt返回给定基中i的字符串表示形式,
对于2strconv.Itoa
是FormatInt(int64(i),10)
的缩写:
Formatt返回给定基中i的字符串表示形式,
对于2首先,如果您想用字符串初始化每个板位置,您需要更改板
数组的声明:
var board [8][8]string
因为strconv.Itoa
返回一个字符串
然后,如果您只需要按顺序访问每个电路板位置,您只需更新内部循环即可:
// 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])
}
}
//不需要像C一样使用索引算术遍历数组
对于i:=0;i<8;i++{//初始化数组
对于j:=0;j<8;j++{
//使用您正在使用的任何逻辑初始化每个值
板[i][j]=(strconv.Itoa(i+64)+“”+strconv.Itoa(j+48))//int到char
fmt.Printf(“%s\n”,板[i][j])
}
}
首先,如果您想用字符串初始化每个板位置,您需要更改板
数组的声明:
var board [8][8]string
因为strconv.Itoa
返回一个字符串
然后,如果您只需要按顺序访问每个电路板位置,您只需更新内部循环即可:
// 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])
}
}
//不需要像C一样使用索引算术遍历数组
对于i:=0;i<8;i++{//初始化数组
对于j:=0;j<8;j++{
//使用您正在使用的任何逻辑初始化每个值
板[i][j]=(strconv.Itoa(i+64)+“”+strconv.Itoa(j+48))//int到char
fmt.Printf(“%s\n”,板[i][j])
}
}
非常感谢!这正是我需要的。非常感谢!这正是我需要的。
var board [8][8]string
// 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])
}
}