最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

go - Sort IMAP email uids based on data Golang - Stack Overflow

programmeradmin2浏览0评论

I am using this IMAP Golang package to fetch recent emails from my gmail. I would like to only get the 50 most recent messages. My current approach is fetching every single email uid. I am using imap.gmail is there anyway to get the most recent messages, or the 50 highest UID's?

func getCode(findEmail string) (string, error) {
    uids, err := im.GetUIDs("1:*")
    if err != nil {
        imapLock.RUnlock()
        return "", err
    }

    if len(uids) > 50 {
        uids = uids[len(uids)-50:]
    }

    emails, err := im.GetEmails(uids...)
    if err != nil {
        return "", err
    }

    for _, email := range emails {
        // do something
    }
}

I am using this IMAP Golang package to fetch recent emails from my gmail. I would like to only get the 50 most recent messages. My current approach is fetching every single email uid. I am using imap.gmail is there anyway to get the most recent messages, or the 50 highest UID's?

func getCode(findEmail string) (string, error) {
    uids, err := im.GetUIDs("1:*")
    if err != nil {
        imapLock.RUnlock()
        return "", err
    }

    if len(uids) > 50 {
        uids = uids[len(uids)-50:]
    }

    emails, err := im.GetEmails(uids...)
    if err != nil {
        return "", err
    }

    for _, email := range emails {
        // do something
    }
}
Share Improve this question asked Feb 21 at 5:07 Ahmed ZaidanAhmed Zaidan 711 gold badge9 silver badges24 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Modify your UID fetch range to "*" or use SORT if supported:

uids, err := im.GetUIDs("50:*") // Fetch last 50 UIDs

or

uids, err := im.GetSortedUIDs("REVERSE DATE", 50) // If sorting is supported
发布评论

评论列表(0)

  1. 暂无评论