I tested the program on both Linux and Mac, and the behavior is consistent. Makes sense, Looks like an expected behavior. Thanks.
Just paste the program as below, package main import ( "fmt" "os" ) func main() { f, err := os.OpenFile("/tmp/db", os.O_RDWR | os.O_CREATE, 0666) if err != nil { panic(err) } if n, err := f.Write([]byte{1, 2, 3, 4, 5}); err != nil || n != 5 { panic (err) } if n, err := f.Write([]byte{6, 7, 8}); err != nil || n != 3 { panic (err) } if err := f.Sync(); err != nil { panic (err) } if err := f.Close(); err != nil { panic(err) } fmt.Println("Done!") } 在2021年12月8日星期三 UTC+8 16:13:31<Brian Candler> 写道: > As a general request, please don't post screen images. They are very hard > to read, especially for (but not exclusively for) those with visual > impairment. They also can't be copy-pasted. > > You haven't said what platform you are running on. However if this is > Linux or another POSIX-compatible system, it's perfectly fine to delete a > file while it's open. The file remains on disk until the last process > which has an open file handle has closed it. > > You can demonstrate this quite easily: > - create a large (multi-gigabyte) file > - check the filesystem free space > - open the file > - delete it > - check the filesystem free space > - close it > - check the filesystem free space > > Only after closing the file will the free space be returned to the > filesystem. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/57d6d209-25a1-4407-b6e2-224a65732d35n%40googlegroups.com.