zoukankan      html  css  js  c++  java
  • golang结构体排序

    喜M拉Y下载音频到手机,使用ximalaya.exe 解密[.x2m]为[.m4a]
    根据文件下载创建时间,顺序重命名文件,方便后续播放。

    源码如下:
    package main

    import (
    "fmt"
    "io/ioutil"
    "os"
    "path"
    "sort"
    "strconv"
    )

    //定义一个通用的结构体
    type Bucket struct {
    Slice []interface{} //承载以任意结构体为元素构成的Slice
    By func(a,b interface{})bool //排序规则函数,当需要对新的结构体slice进行排序时,只需定义这个函数即可
    }
    /*
    定义三个必须方法的准则:接收者不能为指针
    */
    func (this Bucket)Len()int { return len(this.Slice)}

    func (this Bucket)Swap(i,j int){ this.Slice[i],this.Slice[j] = this.Slice[j],this.Slice[i] }

    func (this Bucket)Less(i,j int)bool { return this.By(this.Slice[i], this.Slice[j]) }

    type FileInfo struct {
    name string `json:"name"`
    time int64 `json:"time"`
    }


    func main() {
    filePath := "C:\超品相师\"
    nameName := "超品相师"
    num := int64(1)

    files, _ := ioutil.ReadDir(filePath)

    results := Bucket{}

    for _, f := range files {
    ext := path.Ext(f.Name())
    if ext ==".x2m" {
    u := FileInfo{
    name:f.Name(),
    time: f.ModTime().Unix(),
    }
    results.Slice= append(results.Slice,u)
    //fmt.Println(f.Name(), f.ModTime().Unix())
    }
    }

    time_by := func(a,b interface{})bool {
    return a.(FileInfo).time<b.(FileInfo).time
    }
    results.By= time_by

    sort.Sort(results)
    //fmt.Println(results.Slice)

    for _,n := range results.Slice {

    x2m_name := n.(FileInfo).name
    m4a_name := x2m_name[0:len(x2m_name)-4] + ".m4a"
    fmt.Println(m4a_name)

    b := strconv.FormatInt(num, 10)
    switch {
    case num < 10 && num >= 1:
    b = "000" + b
    case num < 100 && num >= 10:
    b = "00" + b
    case num < 1000 && num >= 100:
    b = "0" + b
    }
    os.Rename(filePath + m4a_name, filePath + nameName + b +".m4a")
    num ++
    }
    }
  • 相关阅读:
    repair table
    利用逻辑备份恢复部分库表
    Web框架理解
    BootStrape基础使用
    jQuery入门
    BOM操作
    DOM操作
    day12 css样式
    JavaScript基础
    day11 前端知识简单总结
  • 原文地址:https://www.cnblogs.com/kuangxiangnice/p/10419389.html
Copyright © 2011-2022 走看看