func (c *Client) write(request *protobuf.Request) chan protobuf.Response { data, err := proto.Marshal(request) if err != nil { log.Printf("Marshaling error: %v\n", err) return nil } length := len(data) lengthBytes := make([]byte, 4) binary.BigEndian.PutUint32(lengthBytes, uint32(length)) // Guarantee squential write of length then protobuf on stream c.connLock.Lock() defer c.connLock.Unlock() _, err = c.conn.Write(lengthBytes) if err != nil { log.Printf("Error writing data: %v\n", err) return nil } _, err = c.conn.Write(data) if err != nil { log.Printf("Error writing data: %v\n", err) return nil } callback := make(chan protobuf.Response) c.pending[request.GetId()] = callback return callback }