I a simple TCP server that accept connection , Get a URL using http.Get and then encode the response using gob. But gob fails to encode http.gzipReader. On encoding it gives following error message:
gob: type not registered for interface: http.gzipReader
My code is given below:
package main
import (
"encoding/gob"
"fmt"
"log"
"net"
"net/http"
"os"
)
const (
CONN_HOST = "localhost"
CONN_PORT = "3333"
CONN_TYPE = "tcp"
)
func main() {
// Listen for incoming connections.
l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
for {
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
go handleRequest(conn)
}
}
func handleRequest(conn net.Conn) {
res, err :=
http.Get("http://info.cern.ch/hypertext/WWW/TheProject.html")
if err != nil {
log.Fatal(err)
} else {
encoder := gob.NewEncoder(conn)
er := encoder.Encode(res)
if er != nil {
fmt.Println(er)
}
}
conn.Write([]byte("Message received."))
conn.Close()
}
Is there any way to encode http.gzipReaderc ?? I saw an example of gob.Register() , but I am trying to registrer http.gzipReaderc . But unable to do so. can any one suggest me solution to this problem ?
gob: type not registered for interface: http.bodyEOFSignal. Are sure that you want to encode the whole part ofhttp.Response? The content (HTML page) ofhttp.Getresponse is available throughBodyfield (which is an interface and the data itself may not yet available immediately or it may be encoded in gzip etc.) and you should read it first into a buffer (or create a wrapper) then passing it to the gob encoder. - putu