// Hey, I want to embed "gophers" folder in the executable binary// Use embed go 1.16 new feature (for embed gophers static files)//go:embed gophersvarembedGopherFilesembed.FS
funcmain(){// Display usage/help messageiflen(os.Args)==1||(len(os.Args)==2&&os.Args[1]=="-h")||(len(os.Args)==2&&os.Args[1]=="--help"){usage:="GopherSay is inspired by Cowsay program.\nGopherSay allow you to display a message said by a cute random Gopher.\n\nUsage:\n gophersay MESSAGE\n\nExample:\n gophersay hello Gopher lovers"fmt.Println(usage)return}elseiflen(os.Args)>1{message:=strings.Join(os.Args[1:]," ")nbChar:=len(message)line:=" "fori:=0;i<=nbChar;i++{line+="-"}fmt.Println(line)fmt.Println("< "+message+" >")fmt.Println(line)fmt.Println(" \\")fmt.Println(" \\")// Generate a random integer depending on get the number of ascii filesrand.Seed(time.Now().UnixNano())randInt:=rand.Intn(getNbOfGopherFiles()-1)// Display random gopher ASCII embed filesfileData,err:=embedGopherFiles.ReadFile("gophers/gopher"+strconv.Itoa(randInt)+".txt")iferr!=nil{log.Fatal("Error during read gopher ascii file",err)}fmt.Println(string(fileData))}}
// Display usage/help messageiflen(os.Args)==1||(len(os.Args)==2&&os.Args[1]=="-h")||(len(os.Args)==2&&os.Args[1]=="--help"){usage:="GopherSay is inspired by Cowsay program.\nGopherSay allow you to display a message said by a cute random Gopher.\n\nUsage:\n gophersay MESSAGE\n\nExample:\n gophersay hello Gopher lovers"fmt.Println(usage)return}
// Generate a random integer depending on get the number of ascii filesrand.Seed(time.Now().UnixNano())randInt:=rand.Intn(getNbOfGopherFiles()-1)
等等……为什么要执行rand.Seed()这个函数?
rand.Intn(int)返回一个介于 0 和 n 之间的非负伪随机数。这很棒,但是……它生成的是一个确定性的数值序列! 因此,为了获得“真正的”随机数,解决方案是使用 `nil`rand.Seed()来初始化默认的随机源。
让我们回到代码,接下来我们要显示可爱的 ASCII 格式 Gopher:
// Display random gopher ASCII embed filesfileData,err:=embedGopherFiles.ReadFile("gophers/gopher"+strconv.Itoa(randInt)+".txt")iferr!=nil{log.Fatal("Error during read gopher ascii file",err)}fmt.Println(string(fileData))
最后,创建一个函数,返回 ASCII Gopher 图像文件的数量:
funcgetNbOfGopherFiles()int{files,err:=embedGopherFiles.ReadDir("gophers")iferr!=nil{log.Fatal("Error during reading gophers folder",err)}nbOfFiles:=0for_,_=rangefiles{nbOfFiles++}returnnbOfFiles}
$ go run main.go
GopherSay is inspired by Cowsay program.
GopherSay allow you to display a message said by a cute random Gopher.
Usage:
gophersay MESSAGE
Example:
gophersay hello Gopher lovers
$ go run main.go --help
GopherSay is inspired by Cowsay program.
GopherSay allow you to display a message said by a cute random Gopher.
Usage:
gophersay MESSAGE
Example:
gophersay hello Gopher lovers
version:"3"tasks:run:desc:Run the appcmds:-GOFLAGS=-mod=mod go run main.gobuild:desc:Build the appcmds:-GOFLAGS=-mod=mod go build -o bin/gophersay main.goclean:desc:Build the appcmds:-rm -rf dist
$ goreleaser init
• Generating .goreleaser.yml file
• config created; please edit accordingly to your needs file=.goreleaser.yml
我们来看一下这个新生成的文件:
# This is an example .goreleaser.yml file with some sane defaults.# Make sure to check the documentation at http://goreleaser.combefore:hooks:# You may remove this if you don't use go modules.-go mod tidy# you may remove this if you don't need go generate-go generate ./...builds:-env:-CGO_ENABLED=0goos:-linux-windows-darwinarchives:-replacements:darwin:Darwinlinux:Linuxwindows:Windows386:i386amd64:x86_64checksum:name_template:'checksums.txt'snapshot:name_template:"{{incpatch.Version}}-next"changelog:sort:ascfilters:exclude:-'^docs:'-'^test:'
这很棒。因为我们go generate的应用程序不需要用到它,所以我们可以删除那一- go generate ./...行代码 ;-)。