Skip to content

Commit d4be4cc

Browse files
authored
Merge pull request #53 from tsingbx/encap
Add doc for encapsulation
2 parents 8e9ceb9 + a0f152d commit d4be4cc

File tree

8 files changed

+86
-2
lines changed

8 files changed

+86
-2
lines changed

212-Composing-Types-by-Struct-Embedding/struct-emb-1.gop

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Go+ does not provide the typical, type-driven notion of subclassing, but it does have the ability to “borrow” pieces of an implementation by embedding types within a struct or interface.
22
// Only interfaces can be embedded within interfaces.
3-
// Only struct can be embedded within struct.
43

54
// The Sample has two struct types, Hello struct and Goodbye struct, each of which implements the Talk interface. And HelloGoodbye struct also implements Talk interface, which it does by combining Hello struct and Goodbye struct into one struct using embedding: it lists the types within the struct but does not give them field names.
65

214-Encapsulation/encap-1.gop

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// 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
2+
3+
// Capitalized Identifiers are exported. The capital letter indicates that this is an exported identifier.
4+
// 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.
5+
6+
// There are five kinds of identifier which can be exported or non-exported.

214-Encapsulation/encap-2.gop

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 1. Export Struct
2+
3+
package company
4+
5+
type PublicCompany struct {
6+
Name string
7+
address string
8+
}
9+
10+
type privateCompany struct {
11+
name string
12+
address string
13+
}
14+
15+
// Here, in company package, the struct PublicCompany is exported, struct privateCompany is non-exported.

214-Encapsulation/encap-3.gop

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 2. Export structure method
2+
3+
package person
4+
5+
type Person struct {
6+
age int
7+
name string
8+
}
9+
10+
func (person *Person) GetAge() int {
11+
return person.age
12+
}
13+
14+
func (person *Person) getName() string {
15+
return person.name
16+
}
17+
18+
// The Person struct's method GetAge() is exported and getName is not exported.

214-Encapsulation/encap-4.gop

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
# 3. Export structure field
3+
4+
package student
5+
6+
type Student struct {
7+
Name string
8+
age int
9+
}
10+
11+
// The Student struct field Name is exported. The Student struct field age is not exported.

214-Encapsulation/encap-5.gop

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# 4. Export function
3+
4+
package MathTools
5+
6+
func Sum(nums ...int) int {
7+
var s int = 0
8+
for _, num := range nums {
9+
s += num
10+
}
11+
return s
12+
}
13+
14+
func average(nums ...int) int {
15+
if len(nums) <= 0 {
16+
return 0
17+
}
18+
return Sum(nums...) / len(nums)
19+
}
20+
21+
println Sum(1, 2, 3), average(1, 2, 3)
22+
23+
println Sum(1, 2, 3)
24+
25+
// The function Sum is exported, and the function average is not exported.
26+

214-Encapsulation/encap-6.gop

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 5. Export variable
2+
3+
package vars
4+
5+
var (
6+
PersonName = "Wang yang"
7+
personAge = 21
8+
)
9+
10+
// The variable PersonName is exported, and the variable personAge is not exported.

214-Encapsulation/encap.gop

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)