duanhu7615 2016-08-03 02:56
浏览 52
已采纳

将Struct数组元素作为参数传递给Go语言中的函数

I have defined a function possiblemoves(), that takes in two integers as parameters, but later I want this function to make a call to all the elements in Struct array recursively I didn't put the terminating condition yet, I will do it once I finish it

Code:

package main

import (
    "fmt"
)

/*type node struct{
     prev node
     current node
      Next [64] int
}*/
type rowcol struct {
    row int
    col int
}

func main() {
    possiblemoves(1, 5)
}
func possiblemoves(row int, col int) {
    var c [8]rowcol
    var a [16]int

    a[0] = row + 1
    a[1] = col - 2
    a[2] = row - 1
    a[3] = col + 2
    a[4] = row + 1
    a[5] = col + 2
    a[6] = row - 1
    a[7] = col - 2
    a[8] = row - 2
    a[9] = col + 1
    a[10] = row - 2
    a[11] = col - 1
    a[12] = row + 2
    a[13] = col - 1
    a[14] = row + 2
    a[15] = col + 1

    for i := 0; i < len(a); i++ {
        if a[i] <= 0 {
            a[i] = 0
        }
        fmt.Println(a[i])
    }

    c[0] = rowcol{a[0], a[1]}
    c[1] = rowcol{a[2], a[3]}
    c[2] = rowcol{a[4], a[5]}
    c[3] = rowcol{a[6], a[7]}
    c[4] = rowcol{a[8], a[9]}
    c[5] = rowcol{a[10], a[11]}
    c[6] = rowcol{a[12], a[13]}
    c[7] = rowcol{a[14], a[15]}

    for j := 0; j < len(c); j++ {
        {
            possiblemoves(c[j])
        }
    }

}
  • 写回答

1条回答 默认 最新

  • dongzhang5787 2016-08-03 03:24
    关注

    Simply do

    type rowcol struct {
        row int
        col int
    }
    
    func possiblemoves(rc []rowcol) {}
    
    func main() {
        rc := []rowcol{
            rowcol{1, 2},
            rowcol{3, 4},
        }
        possiblemoves(rc)
    }
    

    https://play.golang.org/p/dQ1edTJNhq

    []rowcol is a slice of rowcol structs. Then you use rc[1].row and rc[1].col to access these struct fields.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器