linux上的Golang csv写空
我在linux上编写csv文件时遇到问题,使用完全相同的代码,它在windows上工作,但在linux(Centos7)上没有写入任何内容: 描述的Golang规范: OpenFile是广义的open调用;大多数用户将使用Open或 而是创建。它使用指定的标志(Orduonly)打开命名文件 等)和perm(在umask之前),如适用。如果成功,方法是什么 在返回的文件上,可以将其用于I/O。如果有错误,则 将为*PathError类型 您缺少写入使用linux上的Golang csv写空,linux,csv,go,permissions,Linux,Csv,Go,Permissions,我在linux上编写csv文件时遇到问题,使用完全相同的代码,它在windows上工作,但在linux(Centos7)上没有写入任何内容: 描述的Golang规范: OpenFile是广义的open调用;大多数用户将使用Open或 而是创建。它使用指定的标志(Orduonly)打开命名文件 等)和perm(在umask之前),如适用。如果成功,方法是什么 在返回的文件上,可以将其用于I/O。如果有错误,则 将为*PathError类型 您缺少写入使用OpenFile函数创建的文件的标志,这就是
OpenFile
函数创建的文件的标志,这就是文件被打开进行写入或读取,但没有写入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\n", 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\n", e)
os.Exit(1)
}
}
fmt.Println("Done.")
}
源代码中详细说明了以下标志:
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\n", 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\n", e)
os.Exit(1)
}
}
fmt.Println("Done.")
}
// 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.
)