The initial experience of "class" struct in go

Source: Internet
Author: User
// Other packages ................. // Teacher01 can only be referenced in this file. The lower-case type teacher01 struct {ID int name string course string sex string} // teacher02 can be referenced in any file, uppercase type teacher02 struct {ID int name string course string sex string} // Note: // 1) when accessing teacher01, err, cannot refer to unexported name package name. teacher01 // 2) when accessing teacher02.sex, err, implicit assignment of unexported field 'sex' in package name. teacher02 literal // 3) When teacher02.sex is modified, an error is reported when sex is modified. // err, struct variable. sex undefined (cannot refer to unexported field or method sex)
// Code_015_struct_firsttime Project main. gopackage mainimport ("FMT") type student struct {ID int name string sex byte age int ADDR string} func printstudentvalue (TMP student) {TMP. id = 250 FMT. println ("printstudentvalue TMP =", TMP)} func main () {/* 1) Important things three times: the go language does not contain encapsulation, inheritance, polymorphism, and other concepts, however, these features are also implemented in other ways. encapsulation: implemented through methods; inheritance: implemented through anonymous fields; polymorphism: implemented through interfaces 2) if all the members of the struct can be compared, the struct can also be compared, in this case, the two structs can use = or! = Operator, but not supported> or <. 3) * // 1) initialize the sequence. VaR S1 student = student {1, "ck_god", 0, 27, "SZ"} S2: = student {2, "god_girl", 1, 23, "SZ"} // S3: = student {3, "Stranger", "M", 20, "SZ"} // returns an error. The gender is byte, only 0 or 1 // S4: = student {3, "stranger0", 1, 20} // returns an error. Initialization is not specified. Each member must be initialized. // FMT. printf (S1, S2) // error, non-string type cannot print FMT. println (S1, S2) // println can access FMT. printf ("% T, % t", S1, S2) FMT. println ("") FMT. printf ("% v, % v", S1, S2) // 2) specifies that the member initializes FMT. println ("") S5: = student {ID: 2, name: "laowang"} // The uninitialized member is zero // s5.id, if student. ID: an error is returned. --- there is no ID for this method // 3) access the struct member variable FMT. printf ("ID = % d, name = % s, sex = % C, age = % d, ADDR = % s", s5.id, s5.name, s5.sex, s5.age, s5.addr) // 4) pointer variable FMT of struct. println ("") var S8 * student = & Student {8, "wck", 1, 20, "SZ"} S9: = & Student {9, "god_god ", 0, 21, "SZ"} FMT. printf ("% v, % v", S8, S9) // 5) Assign the value of the struct member variable to FMT. println ("") var S10 student S10.id = 2 s10.name = "Haha" s10.sex = 0 FMT. println (S10) // 6) ========================== the struct variable is the pointer variable S11: = new (student) s11.id = 11 s11.name = "www" FMT. println (S11) // 7) Common variables and pointer variable types print var S12 student = student {4, "mmm", 1, 20, "sh"} FMT. printf ("S12 = % v, & S12 = % V \ n", S12, & S12) // 8) two methods for obtaining members: var p * student = & S12 // P. member and (* P ). member operations are equivalent to P. id = 15 (* P ). name = "Zzz" FMT. println (p, * P, S12) // 9) the struct is passed as the value of the function parameter VaR S13 student = student {1, "shenzhenbei", 1, 18, "SZ"} printstudentvalue (S13) // pass the value. Modifications to the form parameter do not affect the object FMT. println ("Main S =", S13) // 10) the struct is passed as a reference to the function parameter printstudentpointer (& S13) FMT. println ("Main S =", S13) // 11) Visibility of struct members, as described in the document header .} Func printstudentpointer (p * student) {P. ID = 250 FMT. println ("printstudentpointer P =", P )}

The output result is as follows:

{1 ck_god 0 27 SZ} {2 god_girl 1 23 SZ} Main. student, Main. student {1 ck_god 0 27 SZ}, {2 god_girl 1 23 SZ} id = 2, name = laowang, sex = & {8 wck 1 20 SZ }, & {9 god_god 0 21 SZ} {2 Haha 0 0} & {11 www 0 0} S12 = {4 Mmm 1 20 sh }, & S12 = & {4 Mmm 1 20 sh} & {15 ZZZ 1 20 sh} {15 ZZZ 1 20 sh} {15 ZZZ 1 20 sh}

The initial experience of "class" struct in go

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.