Invalid character ' \x00 ' after top-level value in the Go language

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.