Skip to content

Instantly share code, notes, and snippets.

@yakuter
Created February 15, 2024 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yakuter/2ff33dce717af25397cbf9033e054232 to your computer and use it in GitHub Desktop.
Save yakuter/2ff33dce717af25397cbf9033e054232 to your computer and use it in GitHub Desktop.
Copy and Movement 6
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func main() {
filePath := "myFile.txt"
// Create reader and writer using io.Pipe
reader, writer := io.Pipe()
// Read the file and write to the pipe in a goroutine
go func() {
file, err := os.Open(filePath)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
// Copy file content to the pipe
_, err = io.Copy(writer, file)
if err != nil {
fmt.Println("Error writing file to the pipe:", err)
return
}
// Close the writer when the writing operation is done
writer.Close()
}()
// Create HTTP request and send file content as body
resp, err := http.Post("http://example.com/upload", "application/octet-stream", reader)
if err != nil {
fmt.Println("Error during HTTP request:", err)
return
}
defer resp.Body.Close()
fmt.Println("HTTP Status:", resp.Status)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment