duanhuang2804
duanhuang2804
2016-12-28 11:24

如何在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 doudou1897 5年前

    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])
    
    }
    
    点赞 评论 复制链接分享