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

how to generate multiple uuid and md5 files in golang

您好,我已经在 golang 中生成了 Md5 和 uuid,但现在我想使用命令行参数为多个文件生成它,所以我到底需要做什么。这就是我生成 md5 和 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\n", err)
        }

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

        md5 := Getmd5(uuid)

        fmt.Printf("Checksum:   %s\n",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};\n",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])

}

谁能帮帮我?

您可以使用os.Args接受命令行参数

os.Args 提供对原始命令行参数的访问。请注意,此切片中的第一个值是程序的路径,os.Args[1:] 包含程序的参数。

您的程序将如下所示,查看 createFilegetNumberOfFiles 函数以及 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\n", 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\n", 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};\n", 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])

}