"Golang" Analysis on the case difference of class attribute

Source: Internet
Author: User

We know that Golang defines classes and related properties through struct structs. A bit of note here is that the first letter of the property's case is different in meaning!

Go is based on the case of the initial letter to determine the permissions that can be accessed. The name of the method name, constant, variable name, or struct, if capitalized, can be accessed by other packages, and if the first letter is lowercase, it can only be used in this package.

It can be simply understood that the initial capitalization is public and the first letter lowercase is private.

But these are not the main points, after all, many people know.

There is a big difference when Super elder brother in writing the project encountered (ashamed Ah, go Foundation is not solid):

if the class property starts with lowercase, its serialization loses the value of the property, and it cannot parse the JSON .

Nonsense, less code.

Package Mainimport ("bytes" "Encoding/gob" "Log" "Encoding/json" "FMT") type people struct {Name string Age int Description string//Note This property differs from the above, with the first letter lowercase}//serialization func (People *people) Serialize () []byte {var result bytes . Buffer Encoder: = gob. Newencoder (&result) Err: = Encoder. Encode (people) if err! = nil{log. Panic (ERR)} return result. Bytes ()}//deserializes func deserializeblock (peoplebytes []byte) *people {var people people decoder: = gob. Newdecoder (bytes. Newreader (peoplebytes)) Err: = decoder. Decode (&people) if err! = Nil {log. Panic (Err)} return &people}func main () {people: = people{"Chaors", "a Good goo" D Boy ",} fmt. Println () fmt. Println ("Pre-serialization:", people)//serialization Peoplebytes: = people. Serialize ()//deserialization out of FMT. Println () fmt. Println ("Deserialization out:", Deserializeblock (peoplebytes)) fmt. Println ()//json resolves if peoplejson, err: = json. Marshal (people); Err= = Nil {fmt. PRINTLN ("JSON parsing:", String (Peoplejson))//description cannot parse}}

Operation Result:

Image.png

.
.
.
.

The internet is disrupting the world and the blockchain is disrupting the internet!

--------------------------------------------------20180703 20:30

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.