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