Go值方法 & 指针方法

Posted kelamoyujuzhen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Go值方法 & 指针方法相关的知识,希望对你有一定的参考价值。



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值方法 & 指针方法的主要内容,如果未能解决你的问题,请参考以下文章

GO 一文搞懂指针和地址值的区别

初识 go 语言:数据类型

Go语言值接收者方法和指针接收者方法

Go小知识新解

go 结构的方法总结

go 指针