Skip to content

Commit 5f03735

Browse files
committed
Add code
1 parent afb6b1d commit 5f03735

40 files changed

+2130
-0
lines changed

concepts/.DS_Store

6 KB
Binary file not shown.

concepts/Installation-and-setup.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Download Install and go lang onto the machine
2+
3+
https://golang.org/dl/
4+
5+
# Download the mac os variant
6+
Open and follow the instructions
7+
# choose install for all users and DON'T change the install location
8+
#enter your password
9+
10+
11+
# check the go version
12+
go version
13+
14+
#show the go command menu
15+
go
16+
17+
#install sublime text, if not installed
18+
# install package control in sublime,
19+
# just go to Tools > install package control
20+
# Open Command pallete - search golang build
21+
22+
23+
#setup the gopath onto mac - this is required for sublime text in our case
24+
# It is required in general too
25+
echo 'export GOPATH=$HOME/go' >> $HOME/.profile
26+
27+
$ source $HOME/.profile
28+
$ go env | grep GOPATH
29+
30+
31+
# update the same settings in sublime text
32+
sublime-text > preferences > package settings > Golang config
33+
#paste in the following
34+
35+
{
36+
"PATH": "/Users/loonycorn/go/bin",
37+
"GOPATH": "/Users/loonycorn/go"
38+
}
39+
40+
#save the file,
41+
# now you can run go files using cmd +b
42+
=================
43+
** Getting the formatter and syntax recommender **
44+
45+
# get into sublime text dir
46+
cd ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/
47+
48+
# clone the repo
49+
git clone https://github.com/DisposaBoy/GoSublime.git
50+
51+
open sublime-text
52+
53+
you will see a change log
54+
55+
cmd + . and cmd + x
56+
57+
then you see a file called margo.go
58+
59+
close the file and open a new file.go and you can see the changes
60+
61+
62+

concepts/src/CombinedInterfaces.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"bytes"
6+
)
7+
8+
func main() {
9+
var wc WriterCloser = NewBufferedWriterCloser()
10+
wc.Write([]byte ("Hello Youtube listeners, this is a test"))
11+
wc.Close()
12+
}
13+
14+
type Writer interface{
15+
Write([]byte) (int, error)
16+
}
17+
18+
type Closer interface{
19+
Close() error
20+
}
21+
22+
type WriterCloser interface {
23+
Writer
24+
Closer
25+
}
26+
27+
type BufferedWriterCloser struct {
28+
buffer *bytes.Buffer
29+
}
30+
31+
func (bwc *BufferedWriterCloser) Write(data []byte) (int, error){
32+
n, err := bwc.buffer.Write(data)
33+
if err != nil{
34+
return 0, err
35+
}
36+
37+
v := make([]byte, 8)
38+
for bwc.buffer.Len() > 8 {
39+
_, err := bwc.buffer.Read(v)
40+
if err != nil {
41+
return 0, err
42+
}
43+
_, err = fmt.Println(string(v))
44+
if err != nil {
45+
return 0, err
46+
}
47+
48+
}
49+
return n, nil
50+
}
51+
52+
func (bwc *BufferedWriterCloser) Close() error{
53+
for bwc.buffer.Len() > 0 {
54+
data := bwc.buffer.Next(8)
55+
_, err := fmt.Println(string(data))
56+
57+
if err != nil{
58+
return err
59+
}
60+
}
61+
return nil
62+
}
63+
func NewBufferedWriterCloser() *BufferedWriterCloser {
64+
return &BufferedWriterCloser{
65+
buffer: bytes.NewBuffer([]byte{}),
66+
}
67+
}
68+
69+
70+
71+
72+
73+

concepts/src/d01-firstProgram.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
To run the program
3+
4+
cd ~/golang
5+
go run src/firstProgram.go
6+
Uncomment the print statement to explain about it.
7+
*/
8+
9+
10+
11+
package main
12+
13+
import "fmt"
14+
15+
func main() {
16+
17+
fmt.Printf("Hello Go!")
18+
//print("This is without formatting")
19+
// println("This similar as the above but prints in newline")
20+
21+
22+
}
23+

concepts/src/d02-primtiveTypes.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import "fmt"
4+
5+
6+
func main() {
7+
8+
9+
var firstName string = "James"
10+
lastName := "Cook"
11+
var age int = 45
12+
13+
const height int32 = 164
14+
15+
var (
16+
weight float64 = 68.42
17+
occupation string = "Hotelier"
18+
married bool = true
19+
)
20+
21+
22+
fmt.Printf("Name: %s %s, Age: %d,\nHeight: %d, Weight: %f\n ",
23+
firstName, lastName, age, height, weight)
24+
25+
26+
fmt.Printf("He works as %s \n Is married?: %t\n", occupation, married)
27+
28+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
4+
import (
5+
"fmt"
6+
"unsafe"
7+
)
8+
9+
func main() {
10+
11+
var length float32 = 3.20
12+
var breadth float64 = 8.45
13+
14+
area := 0.0
15+
16+
fmt.Println("The length of the rectangle is ", length)
17+
fmt.Println("The breadth of the rectangle is ", breadth)
18+
fmt.Printf("The type of variable \"length\" is %T\n", length)
19+
fmt.Printf("The type of variable \"breadth\" is %T\n", breadth)
20+
fmt.Printf("The default type of the float variable is %T\n", area)
21+
22+
//show it without the float64 conversion and then with the conversion
23+
//var area = length * breadth
24+
area = float64(length) * breadth
25+
26+
//fmt.Println("The area of the rectangle is ", area)
27+
fmt.Printf("The area of the rectangle is %.2f", area)
28+
29+
30+
var minAgeToWork int = 14
31+
32+
// represted in USD
33+
var minWage int32 = 1160
34+
35+
var retirementAge int8 = 62
36+
37+
/* The size of the generic int type is platform dependent.
38+
It is 32 bits wide on a 32-bit system and 64-bits wide on
39+
a 64-bit system
40+
*/
41+
42+
//if size is 8 bytes then, it's 64 bits system
43+
fmt.Printf("\nDefault size of int is %d bytes\n\n", unsafe.Sizeof(minAgeToWork))
44+
45+
46+
fmt.Printf("Minimum age to work in United States is %d", minAgeToWork)
47+
fmt.Printf("\nMinimum wage sums to %d Dollars per month", minWage)
48+
fmt.Printf("\nAverage Retirement Age is %d", retirementAge)
49+
50+
51+
// Go doesn't have char type
52+
// There are byte and rune
53+
// byte is for ASCII characters
54+
// rune is for unicode characers
55+
var copyright = '©'
56+
//byte is an alias of uint8
57+
//rune is an alias of int32
58+
var CharacterNotPresent byte = 'Q'
59+
60+
fmt.Printf("\nDefault variable type of character in go is %T \n\n", copyright)
61+
fmt.Printf("Copyright law is Protected under %c copyright Act of 1976. \n", copyright)
62+
fmt.Printf("Fun Fact: Letter '%c' is not present in any of the U.S state names.\n", CharacterNotPresent)
63+
fmt.Printf("Type of letter 'Q' variable is %T \n", CharacterNotPresent)
64+
65+
66+
var x = 5 + 7i
67+
68+
69+
var y = complex(17, 2.3)
70+
71+
fmt.Printf("Type: %T\n", x)
72+
fmt.Printf("Complex number:%G\n", x)
73+
fmt.Printf("Another Complex Num:%G\n", y)
74+
75+
76+
var a = 7 + 3i
77+
var b = 8 + 2i
78+
79+
var sum = a + b
80+
var diff = a - b
81+
var product = a * b
82+
var div = a / b
83+
84+
fmt.Printf("Sum: %G, Diff: %G, Product : %G,",
85+
sum, diff, product)
86+
87+
fmt.Printf(" Division: %.2f\n", div)
88+
89+
90+
}

concepts/src/d04-SimpleIfElse.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
7+
const minAgeToDrive = 16
8+
9+
var enteredAge int;
10+
11+
fmt.Println("Enter your age:")
12+
fmt.Scan(&enteredAge)
13+
14+
// take the opening braces to next line and compile - show the error
15+
/*if (enteredAge >= minAgeToDrive)
16+
{
17+
*/
18+
// you can't take the else to "next line "
19+
if(enteredAge >= minAgeToDrive){
20+
fmt.Println("You are allowed to drive.")
21+
22+
}
23+
else {
24+
waitPeriod := minAgeToDrive - enteredAge
25+
fmt.Println("You are NOT allowed to drive.")
26+
fmt.Printf("You should wait for another %d years", waitPeriod)
27+
}
28+
29+
30+
}
31+

concepts/src/d05-IfElsebranching.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Run the program once. Then, import random package
3+
generate the random number 1 to 10.
4+
Show the initiliazation in the if case statement
5+
*/
6+
package main
7+
8+
import (
9+
"fmt"
10+
"math/rand"
11+
"time"
12+
)
13+
14+
15+
func main() {
16+
17+
var rating int;
18+
19+
//rand.Seed(time.Now().UTC().UnixNano())
20+
fmt.Println("Please rate the service provided on the scale of 10:")
21+
fmt.Scan(&rating)
22+
23+
//if rating = rand.Intn(10) + 1; rating >8 {
24+
if(rating > 8 ) {
25+
fmt.Println("We are Happy that you enjoyed your time here!",
26+
"Please come back again")
27+
} else if(rating > 6 && rating <= 8){
28+
fmt.Println("We are glad that you liked the service.",
29+
"Please tell us how we can improve it.")
30+
} else if(rating >= 4 && rating <= 6){
31+
fmt.Println("Please tell us how we can improve.")
32+
} else{
33+
fmt.Println("We are sorry that You had a terrible experience.",
34+
"Please tell us what went wrong.")
35+
}
36+
37+
}

concepts/src/d06-SwitchCase.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main(){
6+
7+
month := 7
8+
9+
switch month {
10+
// initialize the month in the switch statement
11+
//switch month := 12; month {
12+
case 1:
13+
fmt.Println("New Year's Day")
14+
fmt.Println("Martin Luther King, Jr. Day")
15+
16+
case 2:
17+
fmt.Println("George Washington’s Birthday")
18+
19+
case 5:
20+
fmt.Println("Memorial Day")
21+
22+
case 7:
23+
fmt.Println("Independence Day")
24+
25+
case 9:
26+
fmt.Println("Labor Day")
27+
28+
case 11:
29+
fmt.Println("Veterans Day")
30+
fmt.Println("Thanksgiving Day")
31+
32+
33+
case 12:
34+
fmt.Println("Christmas Day")
35+
36+
default:
37+
fmt.Println("There are no Holidays in this Month as per",
38+
"Company Calender")
39+
40+
}
41+
42+
}

0 commit comments

Comments
 (0)