The design of a project, need from the client, a structure of information to the server, the service side docking received information to verify. What I'm thinking about here is converting the struct to JSON and then passing it to the server, and the server parsing the JSON.
Because the JSON data format is []byte, so in the network transmission does not need the conversion, the direct pass is OK. The other person receives the []byte data, which is JSON data, and the JSON parsing is done.
However, when parsing in the server, the invalid character ' \x00 ' after top-level value is displayed.
struct Code:
Type Message struct {Name stringInfo string}
Attach the client and server code first.
Client:
Tcpaddr, _: = Net. RESOLVETCPADDR ("TCP", "127.0.0.1:9999") conn, err: = Net. DIALTCP ("TCP", nil, TCPADDR) message. Name = "Cloud" message. Info = "LIST" data, _: = json. Marshal (MESSAGE) Conn. Write (data)
Service-Side code:
Read a client sent over data: = Make ([]byte, +) Total, err: = Tcpconn.read (data) var message messageerr = json. Unmarshal (data, &message) if err! = Nil { FMT. PRINTLN (ERR)}
Fmt.println here, it shows invalid character ' \x00 ' after top-level value, which, after analysis, will
Err = json. Unmarshal (data, &message)
Switch
Err = json. Unmarshal (Data[:total], &message)
Can. The reason is that the data read has a length, if not specified, the default will be the last character deleted (My guess, I am not very clear, understand the great god please help answer)
Also, the name of the field in the struct must be capitalized, and I can't figure out why it is like this.
Invalid character ' \x00 ' after top-level value in the Go language