File tree 2 files changed +60
-0
lines changed
return_a_random_greeting/greetings
2 files changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ module aniket-batabyal.com/greetings
2
+
3
+ go 1.20
Original file line number Diff line number Diff line change
1
+ package greetings
2
+
3
+ import (
4
+ "errors"
5
+ "fmt"
6
+ "math/rand"
7
+ "time"
8
+ )
9
+
10
+ func GreetUser (name string ) (string , error ) {
11
+
12
+ if name == "" {
13
+ return "" , errors .New ("the name cannot be empty or nil" )
14
+ }
15
+ message := fmt .Sprintf (randomHello (), name )
16
+ return message , nil
17
+ }
18
+
19
+ func GreetUsers (names []string ) (map [string ]string , error ) {
20
+
21
+ // make the messages map by using the make function
22
+ messages := make (map [string ]string )
23
+
24
+ // loop through the names slice
25
+ for idx , name := range names {
26
+ fmt .Println ("Index is: " , idx )
27
+
28
+ // call greet user and get the message and the error
29
+ message , err := GreetUser (name )
30
+
31
+ // if error is nil then return the error
32
+ if err != nil {
33
+ return nil , err
34
+ }
35
+
36
+ messages [name ] = message
37
+ }
38
+
39
+ return messages , nil
40
+ }
41
+
42
+ func init () {
43
+ rand .Seed (time .Now ().UnixNano ())
44
+ }
45
+
46
+ func randomHello () string {
47
+
48
+ formattedHellos := []string {
49
+ "Hey mate! %v" ,
50
+ "Hola %v" ,
51
+ "konichiwa %v" ,
52
+ }
53
+
54
+ // fmt.Println("The length of the formatted hellos screen is", len(formattedHellos))
55
+
56
+ return formattedHellos [rand .Intn (len (formattedHellos ))]
57
+ }
You can’t perform that action at this time.
0 commit comments