Created
February 15, 2024 17:03
-
-
Save yakuter/2ff33dce717af25397cbf9033e054232 to your computer and use it in GitHub Desktop.
Copy and Movement 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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