Skip to content

Commit 4696a9f

Browse files
committed
add declared-a-function eg
1 parent 6f69415 commit 4696a9f

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
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 string, username string, message ...string) (welcome string, info string) {
21+
22+
/**
23+
* <naming-return-val1> = string
24+
* <naming-return-val2> = string
25+
*/
26+
welcome = "\n" + message[0] + " " + name
27+
info = "You are authorize to access the system: " + username + "\n"
28+
29+
fmt.Println(message[1])
30+
fmt.Println(message[2])
31+
fmt.Println("Number of parameters: ", len(message))
32+
33+
return
34+
}
35+
36+
func Print(s string) {
37+
fmt.Print(s)
38+
}
39+
40+
func PrintLine(s string) {
41+
fmt.Println(s)
42+
}
43+
44+
/**
45+
* Define a Greeting function;
46+
*/
47+
func Greeting(github Profile, do Printer) {
48+
49+
wel, inf := CreateMessage(github.name, github.username, github.message, "Go is concurrent", "Go is awesome")
50+
51+
/**
52+
* Commenting exact below "Println(wel) line would throw an error "wel declared and not used"
53+
* In case you want to ignore the wel declaration and use info => replace wel with _ as below syntax
54+
*
55+
* E.g. _, info := CreateMessage(github.name, github.username, github.message)
56+
*/
57+
do(wel)
58+
do(inf)
59+
60+
// fmt.Println(_) // Cannot use _ as value
61+
}
62+
63+
func main() {
64+
65+
var github = Profile{"Ashwin Hegde", "hegdeashwin", "Welcome to Go world!"}
66+
67+
/**
68+
* Call the function and pass the data to the function
69+
*/
70+
Greeting(github, Print)
71+
Greeting(github, PrintLine)
72+
}

0 commit comments

Comments
 (0)