douji1999 2019-03-02 22:00
浏览 561

从os.Args [1]读取时,golang文件名过长错误

I'm new to golang and using ioutil.ReadFile(os.Args[1]) to fetch a file path from the cli and then processing each line using:

package main

import (
    "bufio"
    "fmt"
    "log"
    "os"
)

func main()  {

    file, err := os.Open(os.Args[1])
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        fmt.Println(scanner.Text())
    }

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}

Now I can get the output at stdout. But also get the following ERROR in the end:

msg:"BRUMBRUM";reference:cve,CVE-2007-2810;reference:blah;
msg:"WAKANDA";reference:cve,CVE-2007-2810;reference:blah; file name too long
exit status 1

My file path input is data/srulz.tcl . FYI, the error message is not a part of the text file.

I need to know where is this going wrong here and how can this be improved?

UPDATE:

Provided issue reproducible code.

Go-ing with flag pkg for now until this mystery is solved

  • 写回答

1条回答 默认 最新

  • douqin1932 2019-03-03 10:38
    关注

    In the first line, filePath, err := ioutil.ReadFile(os.Args[1])

    Above step will read the whole file contents and return slice of byte and error. filePath variable will not store the file path instead its storing the content of file in bytes. I am wondering why are you not getting compile time error as filepath variable is slice of bytes whereas os.Open(filepath) the argument to os.Open will be string.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)