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条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退