Understanding the application of the Golang interface through the use of the sort package

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In the Go language application, which involves sorting, usually implemented using the sort package, the sort package implements 3 basic sorting algorithms: Insert sort, quick row, and heap sort, which is not intended to explore sorting algorithms, but to understand interface applications by using the sort package.

Sort.go

type Interface interface {    // Len is the number of elements in the collection.    Len() int    // Less reports whether the element with    // index i should sort before the element with index j.    Less(i, j int) bool    // Swap swaps the elements with indexes i and j.    Swap(i, j int)}

In the interface interface, these three methods are sort of the demand abstraction, any implementation of the three types of methods, all implemented this interface, you can use the sorting method, to avoid redefining the various parameters of the sort function. Where Len is the number of sets to sort, as a condition for selecting the appropriate sorting method, less is used to determine the size of the two elements of index I and J, and swap is used to exchange two elements.
The sort package has been implemented with []int,[]float64,[]string sorting.

// StringSlice attaches the methods of Interface to []string, sorting in increasing order.type StringSlice []stringfunc (p StringSlice) Len() int           { return len(p) }func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }func (p StringSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

It's all ascending. The application is as follows:

package mainimport "fmt"import "sort"func main() {    a := sort.IntSlice{2, 4, 1, 3}    a.Sort()    b := []int{6, 5, 8, 4}    sort.Ints(b)    fmt.Println(a)    fmt.Println(b)}

If it is a specific structure of the sort, you need to implement the interface. An example of a list of items in descending order of amount is as follows:

package mainimport "fmt"import . "sort"type Item struct {    Id     int32    Name   string    Amount int32}type Items []Itemfunc (items Items) Len() int {    return len(items)}func (items Items) Less(i, j int) bool {    if items[i].Amount < items[j].Amount {        return false    }    return true}func (items Items) Swap(i, j int) {    items[i], items[j] = items[j], items[i]}func main() {    items := Items{{1, "item_0", 3}, {2, "Item_1", 1}, {3, "item_2", 2}}    Sort(items)    fmt.Println(items)}

Operation Result:
[{1 Item_0 3} {3 Item_2 2} {2 Item_1 1}]
Summing up, Golang with interface to achieve class, abstract, polymorphic programming ideas, in peacetime development, multi-interface features to design features to make the code more concise, more reasonable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.