Go: Accessibility of structs

Source: Internet
Author: User
Tags uppercase letter

1. To make a symbol visible to other packages (that is, accessible), you need to define the symbol to start with an uppercase letter ------from Go related books
2.go only restricts accessibility within and outside the package, without limiting the accessibility of different files in the same package

This article discusses the accessibility of three parts of a struct: structure itself, struct variables, member variables for struct variables

When discussing a part, ensure that other parts of the package are accessible.

1. Accessibility of the structure itself

Test1.go:

Package Tttype Test struct {X int}

Main.go:

Package Mainimport ("FMT" "tt") var a = &tt. Test{x:5}func Main () {FMT. Println (a)}//compilation succeeded, output://    &{5}

  If the structure "test" in Test1.go is changed to "Test", the compilation error indicates that "test" is not accessible.  

Note: Modify the "Test" at the same time to modify the Main.go related code, the subsequent tests are modified by default, no longer emphasized.

  Conclusion: The case of struct name affects the accessibility of the structure itself, the first letter lowercase is not visible outside the package (such as defining the structure "test", you cannot define the "test" type of struct- body variable outside the package)

2. Structure variables

Test1.go

Package Tttype Test struct {X Int}var t test

 Main.go

Package Mainimport ("FMT" "tt") func main () {FMT. Println (TT.T)}

  Compile error, prompt tt.t not accessible; change struct variable "T" to "T", program runs normally, output {0}

Conclusion: The first letter case of struct variable name will affect its accessibility outside the package.

3. member variables for struct variables

1) Different packages

Test1.go

Package Tttype test struct {x Int}var T test

  Main.go

Package Mainimport ("FMT" "tt") func main () {FMT. PRINTLN (TT. T.x)}

  Compile error, prompt TT. The t.x is inaccessible; the t member variable "x" is changed to "X", and the program runs normally, output 0 

Conclusion: Ibid.

2) Same Package

Package Mainimport ("FMT") type test struct {x Int}func main () {t: = new (test) FMT. Println (T.x)}

  The program runs normally and outputs 0.

Conclusion: In the same package, the member variables of the struct variables can be accessed at any time, regardless of the case of the first letter (even if the member variable is lowercase, it can be accessed directly without the need for a member function). 

  

 

  

  

Go: Accessibility of structs

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.