-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
70 lines (55 loc) · 1.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
"embed"
)
// 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 gophers
var embedGopherFiles embed.FS
func main() {
// Display usage/help message
if len(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
} else if len(os.Args) > 1 {
message := strings.Join(os.Args[1:], " ")
nbChar := len(message)
line := " "
for i := 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 files
rand.Seed(time.Now().UnixNano())
randInt := rand.Intn(getNbOfGopherFiles() - 1)
// Display random gopher asii embed files
fileData, err := embedGopherFiles.ReadFile("gophers/gopher" + strconv.Itoa(randInt) + ".txt")
if err != nil {
log.Fatal("Error during read gopher ascii file", err)
}
fmt.Println(string(fileData))
}
}
func getNbOfGopherFiles() int {
files, err := embedGopherFiles.ReadDir("gophers")
if err != nil {
log.Fatal("Error during reading gophers folder", err)
}
nbOfFiles := 0
for _, _ = range files {
nbOfFiles++
}
return nbOfFiles
}