英文:
How to verify if file has contents to marshal from ioutil.ReadFile in Go
问题
我正在尝试使用文件而不是数据库来创建一个原型。我有一个程序,它(1)从文件中读取现有内容到一个映射中,(2)接收 JSON POST 请求并将内容添加到映射中,(3)在退出时将内容写入文件。
首先,文件没有被创建。然后我创建了一个空文件,但仍未被写入。
我正在尝试读取文件,确定是否存在现有内容。如果不存在现有内容,则创建一个空映射。如果存在现有内容,则将其解组为一个新的映射。
func writeDB() {
eventDBJSON, err := json.Marshal(eventDB)
if err != nil {
panic(err)
}
err2 := ioutil.WriteFile("/Users/sarah/go/dat.txt", eventDBJSON, 0777)
if err2 != nil {
panic(err2)
}
}
func main() {
dat, err := ioutil.ReadFile("/Users/sarah/go/dat.txt")
if err != nil {
panic(err)
}
if dat == nil {
eventDB = DB{
events: map[string]event{},
}
} else {
if err2 := json.Unmarshal(dat, &eventDB); err2 != nil {
panic(err2)
}
}
router := httprouter.New()
router.POST("/join", JoinEvent)
router.POST("/create", CreateEvent)
log.Fatal(http.ListenAndServe(":8080", router))
defer writeDB()
}
以上是你提供的代码。
英文:
I am trying to use a file instead of a DB to get a prototype up and running. I have a program that (1) reads existing content from the file to a map, (2) takes JSON POSTs that add content to the map, (3) on exit, writes to the file.
First, the file is not being created. Then I created an empty file. It is not being written to.
I am trying to read the file, determine if there is existing content. If there is not existing content, create a blank map. If there is existing content, unmarshal it into a new map.
func writeDB() {
eventDBJSON, err := json.Marshal(eventDB)
if err != nil {
panic(err)
}
err2 := ioutil.WriteFile("/Users/sarah/go/dat.txt", eventDBJSON, 0777)
if err2 != nil {
panic(err2)
}
}
func main() {
dat, err := ioutil.ReadFile("/Users/sarah/go/dat.txt")
if err != nil {
panic(err)
}
if dat == nil {
eventDB = DB{
events: map[string]event{},
}
} else {
if err2 := json.Unmarshal(dat, &eventDB); err2 != nil {
panic(err2)
}
}
router := httprouter.New()
router.POST("/join", JoinEvent)
router.POST("/create", CreateEvent)
log.Fatal(http.ListenAndServe(":8080", router))
defer writeDB()
}
答案1
得分: 1
服务器永远无法达到defer writeDB()
。
http.ListenAndServe
会阻塞,如果它返回任何内容,你会使用log.Fatal
记录错误并退出应用程序。
你无法拦截应用程序可能退出的所有方式,比如收到SIGKILL
信号、机器断电等。
我猜你只是想编写一些代码,重启服务器,然后重复这个过程。
如果是这样的话,那么使用Ctrl-C就足够了。
如果你想在按下Ctrl-C时写入文件,请查看signal包。
另外,在函数的最后一行使用defer
实际上没有任何意义,因为defer
的作用是“在最后执行”。
英文:
There is no way for the server to ever reach defer writeDB()
.
http.ListenAndServe blocks, and if it did return anything, you log.Fatal that, which exits your app at that point.
You can't intercept all ways an app can exit, getting SIGKILL, machine loss of power, etc.
I'm assuming you really just want to write some code, bounce the server, repeat
If that's the case, then Ctrl-C is good enough.
If you want to write your file on Ctrl-C, look at the signal package.
Also, defer on the last line of a function really has no purpose as defer basically means "do this last".
答案2
得分: 0
你可以使用(*os.File).Stat()来获取文件的FileInfo,其中包含文件的大小。
file, err := os.Open(filepath)
if err != nil {
// 处理错误
}
fi, err := file.Stat()
if err != nil {
// 处理错误
}
s := fi.Size()
英文:
you can use (*os.File).Stat() to get a file's FileInfo which contain its size
file, err := os.Open( filepath )
if err != nil {
// handle error
}
fi, err := file.Stat()
if err != nil {
// handle error
}
s := fi.Size()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论