Golang interface best practices

Reference types: pointers, slices, maps, pipes, interfaces, etc. are all reference types. Modifications when used as function parameters will affect the original data.

Implement sorting of Hero structure slices: sort.Sort (data Interface)

There are three methods in the Interface that can be implemented. In fact, if you want to call the methods provided by the system to sort the structure slices, then you need to implement the three methods of the interface.

In fact, the sorting method provided in the interface is used in the sort function. (In fact, the structure slice type implements the three methods of that interface, so you can call the sort method to complete the sorting of the structure slices)

Only the slice type of a structure can store variables of multiple structures.

Software package: sort func Sort(data Interface)

package main

import (
	"fmt"
	"math/rand"
	"sort"
)

// Hero 声明hero结构体
type Hero struct {
	Name string
	Age  int
}

// HeroSlice 声明hero结构体切片类型
type HeroSlice []Hero

// Len 实现Interface接口,将下面的三个方法都实现了。那么就可以调用Sort包里面的Sort方法了
func (hs HeroSlice) Len() int {
	return len(hs)
}

// Less less这个方法就是决定你使用什么标准进行排序
// 按照hero的年龄从小到大排序
func (hs HeroSlice) Less(i, j int) bool {
	return hs[i].Age < hs[j].Age
}

func (hs HeroSlice) Swap(i, j int) {
	temp := hs[i]
	hs[i] = hs[j]
	hs[j] = temp
}

func main() {
	var heroes HeroSlice
	for i := 0; i < 10; i++ {
		hero := Hero{
			Name: fmt.Sprintf("hero~%d", i),
			Age:  rand.Intn(100),
		}
		heroes = append(heroes, hero)
	}

	for _, v := range heroes {
		fmt.Printf("%v", v)
	}

	//调用sort包里面的sort方法,之所以将切片对应的类型放进去,是因为该类型实现了这三个方法
	sort.Sort(heroes)
	fmt.Printf("\n排序后的结果..............\n")
	for _, v := range heroes {
		fmt.Printf("%v", v)
	}

}

{hero~0 99}{hero~1 81}{hero~2 46}{hero~3 33}{hero~4 10}{hero~5 72}{hero~6 43}{hero~7 20}{hero~8 43}{hero~9 18}
排序后的结果..............
{hero~4 10}{hero~9 18}{hero~7 20}{hero~3 33}{hero~6 43}{hero~8 43}{hero~2 46}{hero~5 72}{hero~1 81}{hero~0 99}

If you don’t want to sort by age, but want to sort by name, modify it as follows:

func (hs HeroSlice) Less(i, j int) bool {
	return hs[i].Name < hs[j].Name
}

The wonderful use of the interface is to implement the above method, and then leave the rest to the Sort method in the Sort package. As for how to call these three methods, you don't need to worry about it.

In fact, this method helps you open the interface. If you implement the interface, then the sorting will automatically complete the field. Finally, modify the assignment statement to make it more concise.

func (hs HeroSlice) Swap(i, j int) {
	hs[i], hs[j] = hs[j], hs[i]
}

The above method helps us achieve high-quality code, open some interfaces for others to use, and at the same time leave the core code to the core people to write.

Open the released parts to others and provide interfaces. After using the following methods, you will know what criteria to sort according to. If you can let go, let go; if you can't let go, put it inside.

Guess you like

Origin blog.csdn.net/qq_34556414/article/details/133803514