duanhuang2804 2016-12-28 11:24
浏览 85
已采纳

如何在golang中生成多个uuid和md5文件

Hi I've generated Md5 and uuid in golang but now I want generate it for multiple files using command line arguments, so what exactly I've to do. This is how I've generated my md5 and uuid:

package main

import (
        "crypto/rand"
        "crypto/md5"
        "fmt"
        "io"
        "os"
        "log"
        "text/template"
       )

type Data struct {
    Uuid string
    Md5  string
}

func main() {

    uuid, err := newUUID()
        if err != nil {
            fmt.Printf("error: %v
", err)
        }

    fmt.Printf("UUID:   %s
", uuid)

        md5 := Getmd5(uuid)

        fmt.Printf("Checksum:   %s
",md5)

        fillData := Data{uuid, md5}
    file, err := os.Create("text.txt")
            if err != nil {
                return
            }
            defer file.Close()

    templ, err := template.ParseFiles("template.html")
        if err !=nil{
            log.Fatalln(err)
        }
    err = templ.Execute(file,fillData)
        if err != nil{
            log.Fatalln(err)
        }
}

// newUUID generates a random UUID according to RFC 4122
func newUUID() (string, error) {
uuid := make([]byte, 16)
          n, err := io.ReadFull(rand.Reader, uuid)
          if n != len(uuid) || err != nil {
              return "", err
          }
      // variant bits
      uuid[8] = uuid[8]&^0xc0 | 0x80
          // version 4 (pseudo-random)
          uuid[6] = uuid[6]&^0xf0 | 0x40
          return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
}

func Getmd5(uuid string) (string) {
data := []byte(uuid)
          //md5_buffer := fmt.Sprintf("%x", md5.Sum(data))
          md5_buffer := md5.Sum(data)
          return fmt.Sprintf("{0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x};
",md5_buffer[0:1],
                  md5_buffer[1:2],md5_buffer[2:3],md5_buffer[3:4],md5_buffer[4:5],md5_buffer[5:6],md5_buffer[6:7],md5_buffer[7:8],
                  md5_buffer[8:9],md5_buffer[9:10],md5_buffer[10:11],md5_buffer[11:12],md5_buffer[12:13],md5_buffer[13:14],md5_buffer[14:15],
                  md5_buffer[15:16])

}

Can anyone help me out?

  • 写回答

1条回答 默认 最新

  • doudou1897 2016-12-28 12:51
    关注

    You can use os.Args to accept command line arguements

    os.Args provides access to raw command-line arguments. Note that the first value in this slice is the path to the program, and os.Args[1:] holds the arguments to the program.

    Your program will look like this, have a look at createFile and getNumberOfFiles functions and the main

    package main
    
    import (
        "crypto/md5"
        "crypto/rand"
        "errors"
        "fmt"
        "io"
        "log"
        "os"
        "strconv"
        "text/template"
    )
    
    type Data struct {
        Uuid string
        Md5  string
    }
    
    func createFile(uuid string) {
        md5 := Getmd5(uuid)
        fmt.Printf("Checksum:   %s
    ", md5)
        fillData := Data{uuid, md5}
        file, err := os.Create(uuid + ".txt")
        if err != nil {
            return
        }
        defer file.Close()
    
        templ, err := template.ParseFiles("template.html")
        if err != nil {
            log.Fatalln(err)
        }
        err = templ.Execute(file, fillData)
        if err != nil {
            log.Fatalln(err)
        }
    }
    
    func getNumberOfFiles() (num int, err error) {
        if len(os.Args) == 1 {
            return 0, errors.New("Not enough arguements")
        }
        if num, err = strconv.Atoi(os.Args[1]); err != nil {
            return
        }
        return num, nil
    }
    
    func main() {
        numberOfFiles, err := getNumberOfFiles()
        if err != nil {
            fmt.Println(err.Error())
        }
        fmt.Printf("Creating %d files", numberOfFiles)
        for i := 0; i < numberOfFiles; i++ {
            uuid, err := newUUID()
            if err != nil {
                fmt.Printf("error: %v
    ", err)
            }
            createFile(uuid)
        }
    }
    
    // newUUID generates a random UUID according to RFC 4122
    func newUUID() (string, error) {
        uuid := make([]byte, 16)
        n, err := io.ReadFull(rand.Reader, uuid)
        if n != len(uuid) || err != nil {
            return "", err
        }
        // variant bits
        uuid[8] = uuid[8]&^0xc0 | 0x80
        // version 4 (pseudo-random)
        uuid[6] = uuid[6]&^0xf0 | 0x40
        return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
    }
    
    func Getmd5(uuid string) string {
        data := []byte(uuid)
        //md5_buffer := fmt.Sprintf("%x", md5.Sum(data))
        md5_buffer := md5.Sum(data)
        return fmt.Sprintf("{0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x};
    ", md5_buffer[0:1],
            md5_buffer[1:2], md5_buffer[2:3], md5_buffer[3:4], md5_buffer[4:5], md5_buffer[5:6], md5_buffer[6:7], md5_buffer[7:8],
            md5_buffer[8:9], md5_buffer[9:10], md5_buffer[10:11], md5_buffer[11:12], md5_buffer[12:13], md5_buffer[13:14], md5_buffer[14:15],
            md5_buffer[15:16])
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的