Skip to content

Commit e3a08c5

Browse files
author
Ashwin Hegde
committed
Merge pull request #17 from hegdeashwin/develop
Develop
2 parents d3109dd + 1f1fe2f commit e3a08c5

File tree

3 files changed

+221
-0
lines changed

3 files changed

+221
-0
lines changed

codes/ch2_functions/closures.go

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

0 commit comments

Comments
 (0)