golang-exercises/gob-file/client/main.go

45 lines
572 B
Go
Raw Normal View History

package main
import (
"encoding/gob"
"io"
"log"
"net"
"os"
)
type File struct {
Filename string
File []byte
}
func main() {
conn, err := net.Dial("tcp", "127.0.0.1:1337")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
f, err := os.Open("./gopher.png")
if err != nil {
log.Fatal(err)
}
defer f.Close()
contents, err := io.ReadAll(f)
if err != nil {
log.Fatal(err)
}
fileToSend := File{
Filename: f.Name(),
File: contents,
}
enc := gob.NewEncoder(conn)
err = enc.Encode(&fileToSend)
if err != nil {
log.Fatal(err)
}
}