dtv55860
2016-08-13 13:34
浏览 56
已采纳

将字符串数组的单个元素初始化为Go lang中的另一个字符串变量

In this code, I read a text file for input (A1,B2) and Im using the split function to separate them at comma and store in strs, according to the function definition its returns a array in this case it is strs array, i want the first element in strs to be in currentSource and the second element to be in CurrentDest.I tried printing both the variables individually to check if its working,but the program exits after that and I get a error saying Panic: index out of range. Can anybody help me out..!!!

var currentSource string
var currentDest string

func main() {

    file, err := os.Open("chessin.txt")
    if err != nil {
        fmt.Println(err)
    }
    defer file.Close()
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        strs := strings.Split(scanner.Text(), ",")

        currentSource = strs[0]
        currentDest = strs[1]
}

图片转代码服务由CSDN问答提供 功能建议

在此代码中,我使用分割功能读取了用于输入(A1,B2)和Im的文本文件 它们在逗号处并存储在strs中,根据函数定义,它返回一个数组(在这种情况下为strs数组),我希望strs中的第一个元素在currentSource中,第二个元素在CurrentDest中。 变量分别检查其是否正常工作,但是程序在此之后退出,并且我收到一条错误消息:Panic:索引超出范围。 任何人都可以帮助我.. !!!

  var currentSource字符串
var currentDest字符串
 
func main(){
 
文件,错误:= os  .Open(“ chessin.txt”)
,如果错误!= nil {
 fmt.Println(err)
} 
延迟文件。Close()
扫描程序:= bufio.NewScanner(file)
  Scanner.Scan(){
 strs:= strings.Split(scanner.Text(),“,”)
 
 currentSource = strs [0] 
 currentDest = strs [1] 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongtong848825 2016-08-13 13:52
    已采纳

    This works

    var currentSource string
    var currentDest string
    
    func main() {
    
        file := "A1,B2
    "
        scanner := bufio.NewScanner(strings.NewReader(file))
        for scanner.Scan() {
            strs := strings.Split(scanner.Text(), ",")
    
            currentSource = strs[0]
            currentDest = strs[1]
    
            fmt.Println(strs)
        }
    
    
    }
    

    Are you sure your file (chessin.txt) it is ok?

    Playground

    点赞 评论
  • du127953 2016-08-14 20:29

    This code is almost the same as your code and it works correctly:

    package main
    
    import (
        "bufio"
        "fmt"
        "os"
        "strings"
    )
    
    var currentSource string
    var currentDest string
    
    func main() {
    
        // content of this file is (no spaces between comas, not  or
        // 
     or any other whitespace):
        //C3,F3,C4,A4,C5,A1
        file, err := os.Open("chessin.txt")
        if err != nil {
            fmt.Println(err)
        }
        defer file.Close()
        scanner := bufio.NewScanner(file)
        for scanner.Scan() {
            strs := strings.Split(scanner.Text(), ",")
    
            if len(strs) < 2 {
                panic(`not enough elements in the file, to proceed`)
            }
    
            currentSource = strs[0]
            currentDest = strs[1]
    
            break //(B)
        }
    
        if currentSource != "C3" {
            panic(`currentSource IS NOT C3`)
        }
    
        if currentDest != "F3" {
            panic(`currentDest is not F3`)
        }
    
        // if we are here, then we are good
        fmt.Println(currentSource, currentDest)
    }
    

    Just pay attention to the break statement at (B). That causes the for loop to stop, after the first and second elements of the first line - not sure; but that might be what you want.

    So, if the program does not reach the (A) point, then there something wrong with chessin.txt.

    点赞 评论

相关推荐 更多相似问题