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 diff --git a/codes/ch2_functions/declared-a-function-type.go b/codes/ch2_functions/declared-a-function-type.go new file mode 100644 index 0000000..1b631d8 --- /dev/null +++ b/codes/ch2_functions/declared-a-function-type.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 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