原创 聊聊gost的Pager

发布时间:2021-06-24 18:32:44 浏览 397 来源:猿笔记 作者:go4it

    97


    ##序

    本文主要研究一下gost的Pager

    ##Pager

    gost/hash/page/pager.go//Pageristheabstractionforpaginationusage.

    typePagerinterface{

    \t//GetOffsetwillreturntheoffset

    \tGetOffset()int

    \t//GetPageSizewillreturnthepagesize

    \tGetPageSize()int

    \t//GetTotalPageswillreturnthenumberoftotalpages

    \tGetTotalPages()int

    \t//GetDatawillreturnthedata

    \tGetData()[]interface{}

    \t//GetDataSizewillreturnthesizeofdata.

    \t//Usuallyit'slen(GetData())

    \tGetDataSize()int

    \t//HasNextwillreturnwhetherhasnextpage

    \tHasNext()bool

    \t//HasDatawillreturnwhetherthispagehasdata.

    \tHasData()bool

    }>Pager接口定义了GetOffset、GetPageSize、GetTotalPages、GetData、GetDataSize、HasNext、HasData方法

    ##Page

    gost/hash/page/page.go//PageisthedefaultimplementationofPagerinterface

    typePagestruct{

    \trequestOffsetint

    \tpageSizeint

    \ttotalSizeint

    \tdata[]interface{}

    \ttotalPagesint

    \thasNextbool

    }

    //GetOffSetwillreturntheoffset

    func(d*Page)GetOffset()int{

    \treturnd.requestOffset

    }

    //GetPageSizewillreturnthepagesize

    func(d*Page)GetPageSize()int{

    \treturnd.pageSize

    }

    //GetTotalPageswillreturnthenumberoftotalpages

    func(d*Page)GetTotalPages()int{

    \treturnd.totalPages

    }

    //GetDatawillreturnthedata

    func(d*Page)GetData()[]interface{}{

    \treturnd.data

    }

    //GetDataSizewillreturnthesizeofdata.

    //it'slen(GetData())

    func(d*Page)GetDataSize()int{

    \treturnlen(d.GetData())

    }

    //HasNextwillreturnwhetherhasnextpage

    func(d*Page)HasNext()bool{

    \treturnd.hasNext

    }

    //HasDatawillreturnwhetherthispagehasdata.

    func(d*Page)HasData()bool{

    \treturnd.GetDataSize()>0

    }>Page定义了requestOffset、pageSize、totalSize、data、totalPages、hasNext属性;它实现了Pager接口,其GetDataSize方法返回的是len(d.GetData());其HasData方法使用`d.GetDataSize()>0`来判断

    ##NewPage

    gost/hash/page/page.go//NewPagewillcreateaninstance

    funcNewPage(requestOffsetint,pageSizeint,

    \tdata[]interface{},totalSizeint)*Page{

    \tremain:=totalSize%pageSize

    \ttotalPages:=totalSize/pageSize

    \tifremain>0{

    \t\ttotalPages++

    \t}

    \thasNext:=totalSize-requestOffset-pageSize>0

    \treturn&Page{

    \t\trequestOffset:requestOffset,

    \t\tpageSize:pageSize,

    \t\tdata:data,

    \t\ttotalSize:totalSize,

    \t\ttotalPages:totalPages,

    \t\thasNext:hasNext,

    \t}

    }>NewPage方法根据requestOffset、pageSize、data、totalSize来构造Page;它会计算totalPages、hasNext

    ##实例

    gost/hash/page/page_test.gofuncTestNewDefaultPage(t*testing.T){

    \tdata:=make([]interface{},10)

    \tpage:=NewPage(121,10,data,499)

    \tassert.Equal(t,10,page.GetDataSize())

    \tassert.Equal(t,121,page.GetOffset())

    \tassert.Equal(t,10,page.GetPageSize())

    \tassert.Equal(t,50,page.GetTotalPages())

    \tassert.Equal(t,data,page.GetData())

    \tassert.True(t,page.HasNext())

    \tassert.True(t,page.HasData())

    \tpage=NewPage(492,10,data,499)

    \tassert.False(t,page.HasNext())

    }

    ##小结

    gost的Pager接口定义了GetOffset、GetPageSize、GetTotalPages、GetData、GetDataSize、HasNext、HasData方法;Page定义了requestOffset、pageSize、totalSize、data、totalPages、hasNext属性;它实现了Pager接口。

    ##doc

    -[gost](

作者信息

go4it [等级:3]
发布了 1243 篇专栏 · 获得点赞 844 · 获得阅读 561230

相关推荐 更多