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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?