I am serving a directory using FileServer as follows:
go func() {
fs := http.FileServer(http.Dir("./view"))
err := http.ListenAndServe(":8000", fs)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}()
Inside the view directory I have an index.html file, which I am trying to update while the view directory is being served. I observe that the append commands block and update the file only after I stop serving the directory.
Below is the code to modify the file:
func AppendToFile() {
f, err := os.OpenFile("./view/index.html", os.O_RDWR, 0644)
if err != nil {
panic(err)
}
defer f.Close()
// This assumes that the file ends with </body></html>
f.Seek(-15, 2)
if _, err = f.WriteString("test test test\n"); err != nil {
panic(err)
}
if _, err = f.WriteString("</body></html>\n"); err != nil {
panic(err)
}
}
Is this the expected behavior?
Thank you!
WriteStringat the end of the file.AppendToFile(). See @sigkilled 's answer - as the function should be able to update the file without issue.