This commit is contained in:
chai2010
2016-01-02 21:17:21 +08:00
parent 8772a9c000
commit ba03c527c0
17 changed files with 63 additions and 29 deletions

View File

@@ -5,6 +5,7 @@
Go語言隻有一種控製可見性的手段大寫首字母的標識符會從定義它們的包中被導出小寫字母的則不會。這種限製包內成員的方式同樣適用於struct或者一個類型的方法。因而如果我們想要封裝一個對象我們必鬚將其定義爲一個struct。
這也就是前面的小節中IntSet被定義爲struct類型的原因盡管它隻有一個字段
```go
type IntSet struct {
words []uint64
@@ -12,6 +13,7 @@ type IntSet struct {
```
當然我們也可以把IntSet定義爲一個slice類型盡管這樣我們就需要把代碼中所有方法里用到的s.words用*s替換掉了
```go
type IntSet []uint64
```
@@ -35,7 +37,6 @@ type Buffer struct {
// Grow expands the buffer's capacity, if necessary,
// to guarantee space for another n bytes. [...]
func (b *Buffer) Grow(n int) {
if b.buf == nil {
b.buf = b.initial[:0] // use preallocated space initially