首页 > 代码库 > Go值方法 & 指针方法
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.Interface12 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.Interface12 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.Interface12 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.Interface12 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.Interface12 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.Interface12 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.Interface12 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值方法 & 指针方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。