Encapsulation
Golang provides encapsulation at the package level. Go doesn’t have any public, private or protected keyword. The only mechanism to control the visibility is using the capitalized and non-capitalized formats
Capitalized Identifiers are exported. The capital letter indicates that this is an exported identifier. Non-capitalized identifiers are not exported. The lowercase indicates that the identifier is not exported and will only be accessed from within the same package.
There are five kinds of identifier which can be exported or non-exported.
1. Export Struct
Here, in company package, the struct PublicCompany is exported, struct privateCompany is non-exported.
2. Export structure method
The Person struct’s method GetAge() is exported and getName is not exported.
3. Export structure field
The Student struct field Name is exported. The Student struct field age is not exported.
4. Export function
The function Sum is exported, and the function average is not exported.
5. Export variable
The variable PersonName is exported, and the variable personAge is not exported.
Next example: Interfaces