dongzhuo7291 2018-07-22 08:13 采纳率: 100%
浏览 126
已采纳

Golang CSV在Linux上写为空

I have a problem of writing csv file on linux, with exactly the same code, it works on windows but nothing is written to the file on linux(Centos7):

package main

import (
    "os"
    "fmt"
    "encoding/csv"
)

var data = [][]string{
    {"1","2","3","4","5"},
    {"a","b","c","d","f"},
}


func main() {
    filename := "example.csv"
    fp,e := os.OpenFile(filename, os.O_CREATE|os.O_APPEND, os.ModePerm)
    if nil != e {
        fmt.Printf("Open file '%s' failed: %s
", filename, e)
        os.Exit(1)
    }
    defer fp.Close()
    w := csv.NewWriter(fp)
    defer w.Flush()
    for _,l := range data {
        if e := w.Write(l); nil != e {
            fmt.Printf("Write csv failed: %s
",e)
            os.Exit(1)
        }
    }
    fmt.Println("Done.")
}
  • 写回答

1条回答 默认 最新

  • dstd2129 2018-07-22 08:44
    关注

    Golang spec described OpenFile:

    OpenFile is the generalized open call; most users will use Open or Create instead. It opens the named file with specified flag (O_RDONLY etc.) and perm (before umask), if applicable. If successful, methods on the returned File can be used for I/O. If there is an error, it will be of type *PathError.

    You are missing the flag to write to the file created using OpenFile function that's the reason file is opened for writing or reading but nothing is written to the csv.

    package main
    
    import (
        "encoding/csv"
        "fmt"
        "os"
    )
    
    var data = [][]string{
        {"1", "2", "3", "4", "5"},
        {"a", "b", "c", "d", "f"},
    }
    
    func main() {
        filename := "example.csv"
        fp, e := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModePerm)
        if nil != e {
            fmt.Printf("Open file '%s' failed: %s
    ", filename, e)
            os.Exit(1)
        }
        defer fp.Close()
        w := csv.NewWriter(fp)
        defer w.Flush()
        for _, l := range data {
            if e := w.Write(l); nil != e {
                fmt.Printf("Write csv failed: %s
    ", e)
                os.Exit(1)
            }
        }
        fmt.Println("Done.")
    }
    

    Playground Example

    Flags are elaborated in source code for os/file.go:

    // Flags to OpenFile wrapping those of the underlying system. Not all
    
    // flags may be implemented on a given system.
    
    const (
        // Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified.
        O_RDONLY int = syscall.O_RDONLY // open the file read-only.
        O_WRONLY int = syscall.O_WRONLY // open the file write-only.
        O_RDWR   int = syscall.O_RDWR   // open the file read-write.
        // The remaining values may be or'ed in to control behavior.
        O_APPEND int = syscall.O_APPEND // append data to the file when writing.
        O_CREATE int = syscall.O_CREAT  // create a new file if none exists.
        O_EXCL   int = syscall.O_EXCL   // used with O_CREATE, file must not exist.
        O_SYNC   int = syscall.O_SYNC   // open for synchronous I/O.
        O_TRUNC  int = syscall.O_TRUNC  // if possible, truncate file when opened.
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值