https://github.com/hslam/sendfile
Package sendfile wraps the sendfile system call.
https://github.com/hslam/sendfile
darwin go golang linux sendfile socket unix windows zero-copy zerocopy
Last synced: 5 days ago
JSON representation
Package sendfile wraps the sendfile system call.
- Host: GitHub
- URL: https://github.com/hslam/sendfile
- Owner: hslam
- License: mit
- Created: 2020-11-17T16:14:48.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-11-14T12:56:10.000Z (over 1 year ago)
- Last Synced: 2025-03-30T11:32:44.652Z (about 1 month ago)
- Topics: darwin, go, golang, linux, sendfile, socket, unix, windows, zero-copy, zerocopy
- Language: Go
- Homepage:
- Size: 21.5 KB
- Stars: 10
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sendfile
[](https://pkg.go.dev/github.com/hslam/sendfile)
[](https://github.com/hslam/sendfile/actions)
[](https://codecov.io/gh/hslam/sendfile)
[](https://goreportcard.com/report/github.com/hslam/sendfile)
[](https://github.com/hslam/sendfile/blob/master/LICENSE)Package sendfile wraps the sendfile system call.
## Get started
### Install
```
go get github.com/hslam/sendfile
```
### Import
```
import "github.com/hslam/sendfile"
```
### Usage
#### Example
```go
package mainimport (
"fmt"
"github.com/hslam/sendfile"
"net"
"os"
)func main() {
srcName := "srcfile"
srcFile, err := os.Create(srcName)
if err != nil {
panic(err)
}
defer os.Remove(srcName)
defer srcFile.Close()
contents := "Hello world"
srcFile.Write([]byte(contents))
lis, err := net.Listen("tcp", ":9999")
if err != nil {
panic(err)
}
defer lis.Close()
done := make(chan bool)
go func() {
conn, _ := lis.Accept()
defer conn.Close()
buf := make([]byte, len(contents))
n, _ := conn.Read(buf)
fmt.Println(string(buf[:n]))
close(done)
}()
conn, _ := net.Dial("tcp", "127.0.0.1:9999")
if _, err = sendfile.SendFile(conn, int(srcFile.Fd()), 0, int64(len(contents))); err != nil {
fmt.Println(err)
}
conn.Close()
<-done
}
```### Output
```
Hello world
```### License
This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)### Author
sendfile was written by Meng Huang.