Skip to content

Commit d3391ed

Browse files
author
Ashwin Hegde
committed
Merge pull request #18 from hegdeashwin/develop
If statements + packageing
2 parents e3a08c5 + c559ced commit d3391ed

File tree

4 files changed

+98
-0
lines changed

4 files changed

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

codes/ch3_branching/if.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import "./greeting";
4+
5+
func main() {
6+
7+
var github = greeting.Profile{"Ashwin Hegde", "hegdeashwin", "Welcome to Go world!"}
8+
9+
/**
10+
* Call the function and pass the data to the function
11+
*/
12+
greeting.Greeting(github, greeting.CreatePrintFunction("?"), false)
13+
}

codes/session-3/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)