mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 01:55:35 +08:00
update book code
This commit is contained in:
22
eBook/examples/chapter_10/methodset1.go
Normal file
22
eBook/examples/chapter_10/methodset1.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// methodset1.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type List []int
|
||||
func (l List) Len() int { return len(l) }
|
||||
func (l *List) Append(val int) { *l = append(*l, val) }
|
||||
|
||||
func main() {
|
||||
// A bare value
|
||||
var lst List
|
||||
lst.Append(1)
|
||||
fmt.Printf("%v (len: %d)\n", lst, lst.Len()) // [1] (len: 1)
|
||||
|
||||
// A pointer value
|
||||
plst := new(List)
|
||||
plst.Append(2)
|
||||
fmt.Printf("%v (len: %d)\n", plst, lst.Len()) // &[2] (len: 1)
|
||||
}
|
Reference in New Issue
Block a user