douqi5079 2016-08-11 03:01
浏览 92
已采纳

将函数的返回值分配给Golang中Struct数组中的元素

I want the Struct array to have return values of the Function I defined later in the code.
In here I defined a struct "array" and info is the array of all those values in the struct, I want every element in the info array to have the respective values I mentioned, info.pos should have the value of the string I'm passing through the function PossibleMoves(), info.bitrep should have the return value from the function converttobit(), info.numrep should have the return value of toNumber(), and v1-v8 should have the values of the moves[] array, (v1=moves[0]).
My code is definitely clumsy can someone help?

package main

import ("bufio"
    "fmt"
    "os"
  "strings")

 type array struct{
  pos string
  bitrep int64
numrep,v1,v2,v3,v4,v5,v6,v7,v8 int8
}

func main() {

file, err := os.Open("chessin.txt")
    if err != nil {
        fmt.Println(err)
    }
    defer file.Close()
    scanner := bufio.NewScanner(file)
    valid := []bool{}
    for scanner.Scan() {
        b := strings.Split(scanner.Text(), ",")
        valid = append(valid, isvalid(b))
    }
    fmt.Println(valid)
    info :=[64][11]array {
      info.pos = Possiblemoves(pos)
      info.bitrep=coverttobit(num)
       info.numrep=toNumber(string)
       info.v0=moves[0]
       info.v1=moves[1]
       info.v2=moves[2]
       info.v3=moves[3]
       info.v4=moves[4]
       info.v5=moves[5]
       info.v6=moves[6]
       info.v7=moves[7]

    }
}


func convertingtobit( num int){
  n := int64(num)
  bit:=strconv.FormatInt(n, 2)
}




func isvalid(b string) bool {

  if  b[0]<='H' && b[0]>='A' && b[1]<='8' && b[1]>='0' {
    return true
      }
  return false
}


func toNumber(s string) int {

    if len(s) != 2 {
       fmt.Println("Invalid Input",s,".") 
       } 
      num=int(s[0]-'A')*8 + int(s[1]-'0')
      return num
}

func PossibleMoves(a string) {
  isvalid := isvalid(a)
 if isvalid == true {
var moves [8]string

moves[0]=string(a[0]+1)+string(a[1]+2)
moves[1]=string(a[0]+1)+string(a[1]-2)
moves[2]=string(a[0]-1)+string(a[1]+2)
moves[3]=string(a[0]-1)+string(a[1]-2)
moves[4]=string(a[0]+2)+string(a[1]+1)
moves[5]=string(a[0]+2)+string(a[1]-1)
moves[6]=string(a[0]-2)+string(a[1]+1)
moves[7]=string(a[0]-2)+string(a[1]-1)

fmt.Println("Possible moves are : ",moves)

var PosMoves [8] int


  for i:=0;i<8;i++ {
    if isvalid == true {
    PosMoves[i]=toNumber(moves[i])
  }
}
fmt.Println("After converting : ",PosMoves)

   } else {
    fmt.Println("Invalid Input")
   }
}
  • 写回答

2条回答 默认 最新

  • douliao5550 2016-08-11 06:49
    关注

    Short Answer ( Compile: Success) :

    package main
    
    import "fmt"
    
    type array struct {
        pos                                    string
        bitrep                                 int64
        numrep, v1, v2, v3, v4, v5, v6, v7, v8 int8
    }
    
    func toNumber(s string) int8 {
        if len(s) != 2 {
            fmt.Println("Invalid Input", s, ".")
        }
        num := int8(s[0]-'A')*8 + int8(s[1]-'0')
        return num
    }
    
    func PossibleMoves(out *array, a string) {
        out.v1 = toNumber(string(a[0]+1) + string(a[1]+2))
    }
    
    func main() {
        info := &array{
            pos:    "A1",
            bitrep: 1,
        }
        PossibleMoves(info, "")
    }
    

    There are some problems:

    1- in your code use := instead of = for new vars:

    func toNumber(s string) int {    
        if len(s) != 2 {
           fmt.Println("Invalid Input",s,".") 
           } 
          num=int(s[0]-'A')*8 + int(s[1]-'0')
          return num
    }
    

    Like this:

    func toNumber(s string) int {
        if !isvalid(s) {
            panic("Invalid Input" + s + ".")
        }
        num := int(s[0]-'A')*8 + int(s[1]-'0')
        return num
    }
    

    2- your code:

    func isvalid(b string) bool {
    
      if  b[0]<='H' && b[0]>='A' && b[1]<='8' && b[1]>='0' {
        return true
          }
      return false
    }
    

    check for len(b) == 2 like this:

    func isvalid(b string) bool {
        if len(b) == 2 && b[0] <= 'H' && b[0] >= 'A' && b[1] <= '8' && b[1] >= '1' {
            return true
        }
        return false
    }
    

    3- try not to copy paste:

    moves[0]=string(a[0]+1)+string(a[1]+2)
    moves[1]=string(a[0]+1)+string(a[1]-2)
    moves[2]=string(a[0]-1)+string(a[1]+2)
    moves[3]=string(a[0]-1)+string(a[1]-2)
    moves[4]=string(a[0]+2)+string(a[1]+1)
    moves[5]=string(a[0]+2)+string(a[1]-1)
    moves[6]=string(a[0]-2)+string(a[1]+1)
    moves[7]=string(a[0]-2)+string(a[1]-1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站