1+ package greet
2+
3+ import "fmt"
4+
5+ /**
6+ * User defined type Profile act as struct type
7+ */
8+ type Profile struct {
9+ Name string
10+ Username string
11+ Message string
12+ }
13+
14+ type Printer func (string ) ()
15+ /**
16+ * Define a CreateMessage function;
17+ *
18+ * username is variadic function, can only use ... as final argument in list
19+ */
20+ func CreateMessage (name , username , message string ) (welcome string , info string ) {
21+
22+ /**
23+ * <naming-return-val1> = string
24+ * <naming-return-val2> = string
25+ */
26+ welcome = "\n " + message + " " + name
27+ info = "You are authorize to access the system: " + username + "\n "
28+
29+ return
30+ }
31+
32+ func Print (s string ) {
33+ fmt .Print (s )
34+ }
35+
36+ func PrintLine (s string ) {
37+ fmt .Println (s )
38+ }
39+
40+ func CreatePrintFunction (custom string ) Printer {
41+ return func (s string ) {
42+ fmt .Println (s + custom )
43+ }
44+ }
45+
46+ func GetPrefix (name string ) (prefix string ) {
47+ /**
48+ * By default no break statement required.
49+ * Use fallthrought to continue with the following cases.
50+ * Cases can be expression.
51+ */
52+ switch name {
53+ case "Ashwin Hegde" :
54+ prefix = "Mr. "
55+ fallthrough
56+
57+ /**
58+ * Either `Issac Newton` or `Albert Einstein`.
59+ */
60+ case "Isaac Newton" , "Albert Einstein" :
61+ prefix = "Sir. "
62+
63+ default :
64+ prefix = "Dude. "
65+ }
66+
67+ return
68+ }
69+
70+ /**
71+ * Define a Greet function;
72+ */
73+ func Greet (github Profile , do Printer , isFormat bool ) {
74+
75+ wel , inf := CreateMessage (github .Name , github .Username , github .Message )
76+
77+ /**
78+ * Commenting exact below "Println(wel) line would throw an error "wel declared and not used"
79+ * In case you want to ignore the wel declaration and use info => replace wel with _ as below syntax
80+ *
81+ * E.g. _, info := CreateMessage(github.name, github.username, github.message)
82+ */
83+
84+ /**
85+ * if statement with embedded-statement
86+ * if [var] condition { ... } else { ... }
87+ */
88+ if prefix := GetPrefix (github .Name ); isFormat {
89+ do (prefix + wel )
90+ } else {
91+ do (inf )
92+ }
93+
94+ // fmt.Println(_) // Cannot use _ as value
95+ }
0 commit comments