From 6f69415b41a54db75c1d564d0c5dbc493c2ff7d5 Mon Sep 17 00:00:00 2001 From: hegdeashwin Date: Sat, 20 Jun 2015 23:21:28 +0530 Subject: [PATCH 1/4] add passing-a-function eg --- codes/ch2_functions/passing-a-function.go | 71 +++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 codes/ch2_functions/passing-a-function.go diff --git a/codes/ch2_functions/passing-a-function.go b/codes/ch2_functions/passing-a-function.go new file mode 100644 index 0000000..1918107 --- /dev/null +++ b/codes/ch2_functions/passing-a-function.go @@ -0,0 +1,71 @@ +package main + +import "fmt" + +/** + * User defined type Profile act as struct type + */ +type Profile struct { + name string + username string + message string +} + +/** + * Define a CreateMessage function; + * + * username is variadic function, can only use ... as final argument in list + */ +func CreateMessage(name string, username string, message ...string) (welcome string, info string) { + + /** + * = string + * = string + */ + welcome = "\n" + message[0] + " " + name + info = "You are authorize to access the system: " + username + "\n" + + fmt.Println(message[1]) + fmt.Println(message[2]) + fmt.Println("Number of parameters: ", len(message)) + + return +} + +func Print(s string) { + fmt.Print(s) +} + +func PrintLine(s string) { + fmt.Println(s) +} + +/** + * Define a Greeting function; + */ +func Greeting(github Profile, do func(string)) { + + wel, inf := CreateMessage(github.name, github.username, github.message, "Go is concurrent", "Go is awesome") + + /** + * Commenting exact below "Println(wel) line would throw an error "wel declared and not used" + * In case you want to ignore the wel declaration and use info => replace wel with _ as below syntax + * + * E.g. _, info := CreateMessage(github.name, github.username, github.message) + */ + do(wel) + do(inf) + + // fmt.Println(_) // Cannot use _ as value +} + +func main() { + + var github = Profile{"Ashwin Hegde", "hegdeashwin", "Welcome to Go world!"} + + /** + * Call the function and pass the data to the function + */ + Greeting(github, Print) + Greeting(github, PrintLine) +} \ No newline at end of file From 4696a9f6d0d44185df56aecfe185c2095201b20d Mon Sep 17 00:00:00 2001 From: hegdeashwin Date: Sat, 20 Jun 2015 23:25:12 +0530 Subject: [PATCH 2/4] add declared-a-function eg --- codes/ch2_functions/declared-a-function.go | 72 ++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 codes/ch2_functions/declared-a-function.go diff --git a/codes/ch2_functions/declared-a-function.go b/codes/ch2_functions/declared-a-function.go new file mode 100644 index 0000000..1b631d8 --- /dev/null +++ b/codes/ch2_functions/declared-a-function.go @@ -0,0 +1,72 @@ +package main + +import "fmt" + +/** + * User defined type Profile act as struct type + */ +type Profile struct { + name string + username string + message string +} + +type Printer func(string) () +/** + * Define a CreateMessage function; + * + * username is variadic function, can only use ... as final argument in list + */ +func CreateMessage(name string, username string, message ...string) (welcome string, info string) { + + /** + * = string + * = string + */ + welcome = "\n" + message[0] + " " + name + info = "You are authorize to access the system: " + username + "\n" + + fmt.Println(message[1]) + fmt.Println(message[2]) + fmt.Println("Number of parameters: ", len(message)) + + return +} + +func Print(s string) { + fmt.Print(s) +} + +func PrintLine(s string) { + fmt.Println(s) +} + +/** + * Define a Greeting function; + */ +func Greeting(github Profile, do Printer) { + + wel, inf := CreateMessage(github.name, github.username, github.message, "Go is concurrent", "Go is awesome") + + /** + * Commenting exact below "Println(wel) line would throw an error "wel declared and not used" + * In case you want to ignore the wel declaration and use info => replace wel with _ as below syntax + * + * E.g. _, info := CreateMessage(github.name, github.username, github.message) + */ + do(wel) + do(inf) + + // fmt.Println(_) // Cannot use _ as value +} + +func main() { + + var github = Profile{"Ashwin Hegde", "hegdeashwin", "Welcome to Go world!"} + + /** + * Call the function and pass the data to the function + */ + Greeting(github, Print) + Greeting(github, PrintLine) +} \ No newline at end of file From ec9020cba29fed1ddf50c72e6c67b09d8e891e6f Mon Sep 17 00:00:00 2001 From: hegdeashwin Date: Sat, 20 Jun 2015 23:29:11 +0530 Subject: [PATCH 3/4] renames --- .../{declared-a-function.go => declared-a-function-type.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename codes/ch2_functions/{declared-a-function.go => declared-a-function-type.go} (100%) diff --git a/codes/ch2_functions/declared-a-function.go b/codes/ch2_functions/declared-a-function-type.go similarity index 100% rename from codes/ch2_functions/declared-a-function.go rename to codes/ch2_functions/declared-a-function-type.go From 1f1fe2fb4a0716d5ad8e0071ff0496ea44d7c624 Mon Sep 17 00:00:00 2001 From: hegdeashwin Date: Sat, 20 Jun 2015 23:42:18 +0530 Subject: [PATCH 4/4] add closures eg --- codes/ch2_functions/closures.go | 78 +++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 codes/ch2_functions/closures.go diff --git a/codes/ch2_functions/closures.go b/codes/ch2_functions/closures.go new file mode 100644 index 0000000..4ae8901 --- /dev/null +++ b/codes/ch2_functions/closures.go @@ -0,0 +1,78 @@ +package main + +import "fmt" + +/** + * User defined type Profile act as struct type + */ +type Profile struct { + name string + username string + message string +} + +type Printer func(string) () +/** + * Define a CreateMessage function; + * + * username is variadic function, can only use ... as final argument in list + */ +func CreateMessage(name string, username string, message ...string) (welcome string, info string) { + + /** + * = string + * = string + */ + welcome = "\n" + message[0] + " " + name + info = "You are authorize to access the system: " + username + "\n" + + fmt.Println(message[1]) + fmt.Println(message[2]) + fmt.Println("Number of parameters: ", len(message)) + + return +} + +func Print(s string) { + fmt.Print(s) +} + +func PrintLine(s string) { + fmt.Println(s) +} + +func CreatePrintFunction(custom string) Printer { + return func(s string) { + fmt.Println(s + custom) + } +} + + +/** + * Define a Greeting function; + */ +func Greeting(github Profile, do Printer) { + + wel, inf := CreateMessage(github.name, github.username, github.message, "Go is concurrent", "Go is awesome") + + /** + * Commenting exact below "Println(wel) line would throw an error "wel declared and not used" + * In case you want to ignore the wel declaration and use info => replace wel with _ as below syntax + * + * E.g. _, info := CreateMessage(github.name, github.username, github.message) + */ + do(wel) + do(inf) + + // fmt.Println(_) // Cannot use _ as value +} + +func main() { + + var github = Profile{"Ashwin Hegde", "hegdeashwin", "Welcome to Go world!"} + + /** + * Call the function and pass the data to the function + */ + Greeting(github, CreatePrintFunction("?")) +} \ No newline at end of file