Golang : panic: runtime error: invalid memory address or nil pointer dereference using bufio. 扫描器

标签 go scanning

我正在实现一个使用 bufio.Scanner 和 bufio.Writer 的 go 程序,我已经将我的代码打包如下

package main

import (
    "fmt"
    "player/command"
    "strings"
)

func main() {
    //Enter your code here. Read input from STDIN. Print output to STDOUT



    for commands.Scanner.Scan() {

        //scan a new line and send it to comand variable to check command exist or not
        input := strings.Split(strings.Trim(commands.Scanner.Text(), " "), " ")
        command := input[0]

        if command == "" {
            fmt.Printf("$ %s:", commands.Pwd)
            continue
        }

        if !commands.Commands[command] {
            commands.ThrowError("CANNOT RECOGNIZE INPUT.")

        } else {

            commands.Execute(command, input[1:], nil)

        }
        fmt.Printf("$ %s:", commands.Pwd)
    }
}

我也在主包中使用 init.go 文件,如下所示

package main

import (
    "flag"
    "player/source"
)

func init() {
    sourceFlag := flag.String("filename", "", "if input is through source file")
    flag.Parse()
    if *sourceFlag != "" {
        source.Input(*sourceFlag)
    }
}

我的最终包播放器/源如下:-

package source

    import (
        "bufio"
        "log"
        "os"
        "player/command"
    )

    func Input(source string) {
        if source != "" {
            readFile, err := os.OpenFile(source, os.O_RDONLY, os.ModeExclusive)
            if err != nil {
                log.Fatal(err)
            }
            commands.Scanner = bufio.NewScanner(readFile)
            writeFile, err := os.Create(source + "_output.txt")
            if err != nil {
                log.Fatal(err)
            }
            commands.Writer = bufio.NewWriter(writeFile)
        } else {
            commands.Scanner = bufio.NewScanner(os.Stdin)
            commands.Writer = bufio.NewWriter(os.Stdout)
            // fmt.Println(commands.Scanner)
        }
    }

执行这段代码的结果是

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x58 pc=0x4a253a]

goroutine 1 [running]:
bufio.(*Scanner).Scan(0x0, 0x5)
    /usr/local/go/src/bufio/scan.go:120 +0x2a
main.main()
    /home/xyz/dev/go/src/players/main.go:13 +0x124

即使在初始化扫描仪之后我也不知道为什么我无法读取它

最佳答案

command.Scanner 未初始化的一个原因可能是您没有将 filename 参数传递给主脚本。在这种情况下,根据 if 条件,永远不会调用 source.Input(*sourceFlag)(if *sourceFlag != "" 在缺少 文件名选项)。

此外,由于稍后在 source 中检查空文件名,因此 maininit 中的这个条件是多余的。尝试:

func init() {
    sourceFlag := flag.String("filename", "", "if input is through source file")
    flag.Parse()
    source.Input(*sourceFlag)
}

关于Golang : panic: runtime error: invalid memory address or nil pointer dereference using bufio. 扫描器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38807855/

相关文章:

python - 矩阵上的 tensorflow 扫描

ios - BLE后台扫描iOS

java - Alfresco 社区版中的扫描功能?

vb.net - 使用 WIA 和 VB 从同一台计算机上的多个扫描仪自动扫描

web - 实现 quagga 扫描条形码

go - 将嵌套结构中的 slice 传递给 Go 中的 Revel 模板

去解码图像不支持的类型错误

postgresql - 如果不使用,数据库会挂起

golang 同步/原子包?

postgresql - 使用 SQLX 转到可选字段