package main import ( "io" "os" ) func writefile(f *os.File, s io.ReadCloser, contentlength int64) error { buffer := make([]byte, 4096) eof := false sz := int64(0) defer f.Sync() for !eof { n, err := s.Read(buffer) if err != nil && err != io.EOF { return err } else if err == io.EOF { eof = true } /* ensure we don't write more than expected */ r := int64(n) if sz+r > contentlength { r = contentlength - sz eof = true } _, err = f.Write(buffer[:r]) if err != nil { return err } sz += r } return nil }