码迷,mamicode.com
首页 > 其他好文 > 详细

Go值方法 & 指针方法

时间:2017-06-24 23:40:52      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:tab   值方法   tables   interface   style   int   print   指针方法   ret   



1
package main 2 3 import ( 4 "fmt" 5 "sort" 6 ) 7 8 type SortableStrings [3]string 9 10 type Sortable interface { 11 sort.Interface 12 Sort() 13 } 14 15 func (self SortableStrings) Len() int { 16 return len(self) 17 } 18 19 func (self SortableStrings) Less(i, j int) bool { 20 return self[i] < self[j] 21 } 22 23 func (self SortableStrings) Swap(i, j int) { 24 self[i], self[j] = self[j], self[i] 25 } 26 27 func main() { 28 _, ok1 := interface{}(SortableStrings{}).(sort.Interface) 29 fmt.Println("ok1", ok1) 30 31 _, ok2 := interface{}(SortableStrings{}).(Sortable) 32 fmt.Println("ok2", ok2) 33 }

技术分享


 1 package main
 2 
 3 import (
 4     "fmt"
 5     "sort"
 6 )
 7 
 8 type SortableStrings [3]string
 9 
10 type Sortable interface {
11     sort.Interface
12     Sort()
13 }
14 
15 func (self SortableStrings) Len() int {
16     return len(self)
17 }
18 
19 func (self SortableStrings) Less(i, j int) bool {
20     return self[i] < self[j]
21 }
22 
23 func (self SortableStrings) Swap(i, j int) {
24     self[i], self[j] = self[j], self[i]
25 }
26 
27 func (self SortableStrings) Sort() {
28     sort.Sort(self)
29 }
30 
31 func main() {
32     _, ok1 := interface{}(SortableStrings{}).(sort.Interface)
33     fmt.Println("ok1", ok1)
34 
35     _, ok2 := interface{}(SortableStrings{}).(Sortable)
36     fmt.Println("ok2", ok2)
37 }

技术分享


 1 package main
 2 
 3 import (
 4     "fmt"
 5     "sort"
 6 )
 7 
 8 type SortableStrings [3]string
 9 
10 type Sortable interface {
11     sort.Interface
12     Sort()
13 }
14 
15 func (self SortableStrings) Len() int {
16     return len(self)
17 }
18 
19 func (self SortableStrings) Less(i, j int) bool {
20     return self[i] < self[j]
21 }
22 
23 func (self SortableStrings) Swap(i, j int) {
24     self[i], self[j] = self[j], self[i]
25 }
26 
27 func (self *SortableStrings) Sort() {
28     sort.Sort(self)
29 }
30 
31 func main() {
32     _, ok1 := interface{}(SortableStrings{}).(sort.Interface)
33     fmt.Println("ok1", ok1)
34 
35     _, ok2 := interface{}(SortableStrings{}).(Sortable)
36     fmt.Println("ok2", ok2)
37 }

技术分享


 1 package main
 2 
 3 import (
 4     "fmt"
 5     "sort"
 6 )
 7 
 8 type SortableStrings [3]string
 9 
10 type Sortable interface {
11     sort.Interface
12     Sort()
13 }
14 
15 func (self SortableStrings) Len() int {
16     return len(self)
17 }
18 
19 func (self SortableStrings) Less(i, j int) bool {
20     return self[i] < self[j]
21 }
22 
23 func (self SortableStrings) Swap(i, j int) {
24     self[i], self[j] = self[j], self[i]
25 }
26 
27 func (self *SortableStrings) Sort() {
28     sort.Sort(self)
29 }
30 
31 func main() {
32     _, ok1 := interface{}(SortableStrings{}).(sort.Interface)
33     fmt.Println("ok1", ok1)
34 
35     _, ok2 := interface{}(&SortableStrings{}).(Sortable)
36     fmt.Println("ok2", ok2)
37 }

技术分享


 1 package main
 2 
 3 import (
 4     "fmt"
 5     "sort"
 6 )
 7 
 8 type SortableStrings [3]string
 9 
10 type Sortable interface {
11     sort.Interface
12     Sort()
13 }
14 
15 func (self SortableStrings) Len() int {
16     return len(self)
17 }
18 
19 func (self SortableStrings) Less(i, j int) bool {
20     return self[i] < self[j]
21 }
22 
23 func (self SortableStrings) Swap(i, j int) {
24     self[i], self[j] = self[j], self[i]
25 }
26 
27 func (self *SortableStrings) Sort() {
28     sort.Sort(self)
29 }
30 
31 func main() {
32     _, ok1 := interface{}(&SortableStrings{}).(sort.Interface)
33     fmt.Println("ok1", ok1)
34 
35     _, ok2 := interface{}(&SortableStrings{}).(Sortable)
36     fmt.Println("ok2", ok2)
37 }

技术分享


 1 package main
 2 
 3 import (
 4     "fmt"
 5     "sort"
 6 )
 7 
 8 type SortableStrings [3]string
 9 
10 type Sortable interface {
11     sort.Interface
12     Sort()
13 }
14 
15 func (self SortableStrings) Len() int {
16     return len(self)
17 }
18 
19 func (self SortableStrings) Less(i, j int) bool {
20     return self[i] < self[j]
21 }
22 
23 func (self SortableStrings) Swap(i, j int) {
24     self[i], self[j] = self[j], self[i]
25 }
26 
27 func (self *SortableStrings) Sort() {
28     sort.Sort(self)
29 }
30 
31 func main() {
32     ss := SortableStrings{"2", "3", "1"}
33     ss.Sort()
34     fmt.Println("Sortable Strings", ss)
35     _, ok1 := interface{}(SortableStrings{}).(sort.Interface)
36     fmt.Println("ok1", ok1)
37 
38     _, ok2 := interface{}(SortableStrings{}).(Sortable)
39     fmt.Println("ok2", ok2)
40 
41     _, ok3 := interface{}(&SortableStrings{}).(sort.Interface)
42     fmt.Println("ok3", ok3)
43 
44     _, ok4 := interface{}(&SortableStrings{}).(Sortable)
45     fmt.Println("ok4", ok4)
46 }

技术分享


 1 package main
 2 
 3 import (
 4     "fmt"
 5     "sort"
 6 )
 7 
 8 type SortableStrings [3]string
 9 
10 type Sortable interface {
11     sort.Interface
12     Sort()
13 }
14 
15 func (self *SortableStrings) Len() int {
16     return len(self)
17 }
18 
19 func (self *SortableStrings) Less(i, j int) bool {
20     return self[i] < self[j]
21 }
22 
23 func (self *SortableStrings) Swap(i, j int) {
24     self[i], self[j] = self[j], self[i]
25 }
26 
27 func (self *SortableStrings) Sort() {
28     sort.Sort(self)
29 }
30 
31 func main() {
32     ss := SortableStrings{"2", "3", "1"}
33     ss.Sort()
34     fmt.Println("Sortable Strings", ss)
35     _, ok1 := interface{}(SortableStrings{}).(sort.Interface)
36     fmt.Println("ok1", ok1)
37 
38     _, ok2 := interface{}(SortableStrings{}).(Sortable)
39     fmt.Println("ok2", ok2)
40 
41     _, ok3 := interface{}(&SortableStrings{}).(sort.Interface)
42     fmt.Println("ok3", ok3)
43 
44     _, ok4 := interface{}(&SortableStrings{}).(Sortable)
45     fmt.Println("ok4", ok4)
46 }

技术分享

 

Go值方法 & 指针方法

标签:tab   值方法   tables   interface   style   int   print   指针方法   ret   

原文地址:http://www.cnblogs.com/kelamoyujuzhen/p/7074779.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!