golang-exercises/gob-listfiles/main.go

48 lines
610 B
Go

package main
import (
"encoding/gob"
"fmt"
"log"
"net"
)
type FileList struct {
List []Item
}
type Item struct {
Name string
IsFolder bool
}
func main() {
fmt.Println("Listening on port 1337")
ln, err := net.Listen("tcp", ":1337")
if err != nil {
log.Fatal(err)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Println(err)
}
go receiveFile(conn)
}
}
func receiveFile(conn net.Conn) {
dec := gob.NewDecoder(conn)
c := FileList{}
err := dec.Decode(&c)
if err != nil {
log.Println(err)
return
}
for _, v := range c.List {
fmt.Println(v.Name, v.IsFolder)
}
}