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 }
时间: 2024-10-12 17:42:31