Go language Sorting and interface instance analysis _golang

Source: Internet
Author: User

The example in this article describes the go language sorting and interface usage. Share to everyone for your reference. Specifically as follows:

Copy Code code as follows:
Import "FMT"
Type Sorter Interface {
Len () int
Less (i, J int) bool
Swap (i, J int)
}
Type Xi []int
Type Xs []string
Func (P Xi) len () int {return Len (p)}
Func (P Xi) less (i int, j int) bool {return P[J] < P[i]}
Func (P Xi) Swap (i int, j int) {P[i], p[j] = P[j], P[i]}
Func (P Xs) Len () int {return Len (p)}
Func (P Xs) less (i int, j int) bool {return P[J] < P[i]}
Func (P Xs) Swap (i int, j int) {P[i], p[j] = P[j], P[i]}
Func Sort (x Sorter) {
For I: = 0; I < X.len ()-1; i++ {
For J: = i + 1; J < X.len (); J + + {
If X.less (i, j) {
X.swap (i, J)
}
}
}
}
Func Main () {
INTs: = xi{44, 67, 3, 17, 89, 10, 73, 9, 14, 8}
Strings: = xs{"nut", "ape", "Elephant", "Zoo", "Go"}
Sort (INTs)
Fmt. Printf ("%v\n", INTs)
Sort (Strings)
Fmt. Printf ("%v\n", strings)
}

I hope this article will help you with your go language program.

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.