mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-08-12 17:52:39 +00:00
fmr code
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user