Skip to content

Commit

Permalink
Merge pull request #17 from hegdeashwin/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Ashwin Hegde committed Jun 20, 2015
2 parents d3109dd + 1f1fe2f commit e3a08c5
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 0 deletions.
78 changes: 78 additions & 0 deletions codes/ch2_functions/closures.go
Original file line number Diff line number Diff line change
@@ -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) {

/**
* <naming-return-val1> = string
* <naming-return-val2> = 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("?"))
}
72 changes: 72 additions & 0 deletions codes/ch2_functions/declared-a-function-type.go
Original file line number Diff line number Diff line change
@@ -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) {

/**
* <naming-return-val1> = string
* <naming-return-val2> = 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)
}
71 changes: 71 additions & 0 deletions codes/ch2_functions/passing-a-function.go
Original file line number Diff line number Diff line change
@@ -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) {

/**
* <naming-return-val1> = string
* <naming-return-val2> = 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)
}

0 comments on commit e3a08c5

Please sign in to comment.