how to generate multiple uuid and md5 files in golang

huangapple go评论132阅读模式
英文:

how to generate multiple uuid and md5 files in golang

问题

你好,我已经在Go语言中生成了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 := 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])
}

有人可以帮助我吗?

英文:

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\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])
}

Can anyone help me out?

答案1

得分: 0

你可以使用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 arguments")
	}
	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 := 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])

}
英文:

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 (
&quot;crypto/md5&quot;
&quot;crypto/rand&quot;
&quot;errors&quot;
&quot;fmt&quot;
&quot;io&quot;
&quot;log&quot;
&quot;os&quot;
&quot;strconv&quot;
&quot;text/template&quot;
)
type Data struct {
Uuid string
Md5  string
}
func createFile(uuid string) {
md5 := Getmd5(uuid)
fmt.Printf(&quot;Checksum:   %s\n&quot;, md5)
fillData := Data{uuid, md5}
file, err := os.Create(uuid + &quot;.txt&quot;)
if err != nil {
return
}
defer file.Close()
templ, err := template.ParseFiles(&quot;template.html&quot;)
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(&quot;Not enough arguements&quot;)
}
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(&quot;Creating %d files&quot;, numberOfFiles)
for i := 0; i &lt; numberOfFiles; i++ {
uuid, err := newUUID()
if err != nil {
fmt.Printf(&quot;error: %v\n&quot;, 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 &quot;&quot;, err
}
// variant bits
uuid[8] = uuid[8]&amp;^0xc0 | 0x80
// version 4 (pseudo-random)
uuid[6] = uuid[6]&amp;^0xf0 | 0x40
return fmt.Sprintf(&quot;%x-%x-%x-%x-%x&quot;, 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(&quot;%x&quot;, md5.Sum(data))
md5_buffer := md5.Sum(data)
return fmt.Sprintf(&quot;{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&quot;, 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])
}

huangapple
  • 本文由 发表于 2016年12月28日 19:24:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/41361070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定