This is a simple Go project that demonstrates how to send and receive files using Go. The project has two modes: CLI and HTTP.
- Go (version 1.16 or higher)
-
Initialize the Go module:
go mod init file_handler
-
Download the dependencies:
go mod tidy
- Flags are used to choose between sending and receiving (
mode
flag). - If the
file
flag is provided, the program sends the specified file. - If no
file
is specified, the server starts and listens for incoming files.
- The Gin framework is used to handle POST requests at the
/upload
endpoint. - Files are uploaded using multipart form data and saved to the specified directory.
- Start the server to receive files:
go run main.go --mode cli --save-dir /path/to/save
- Send a file:
go run main.go --mode cli --file /path/to/demo.mp4 --address 0.0.0.0:8080
- Start the HTTP server:
go run main.go --mode http --save-dir /path/to/save
- Upload a file via HTTP POST:
curl -X POST -F "file=@/path/to/demo.mp4" http://0.0.0.0:8080/upload
To build the project, use the following command:
go build -o file_handler main.go