Skip to content

Commit 32f4e52

Browse files
authored
Add files via upload
0 parents  commit 32f4e52

File tree

28 files changed

+1052
-0
lines changed

28 files changed

+1052
-0
lines changed

Array/array.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
//Arrays & slices
9+
10+
//Static Array
11+
numsstatic := [3]int{97, 85, 93}
12+
13+
//Unspecified
14+
var wordsunspecified [3]string
15+
wordsunspecified[1] = "John"
16+
17+
var mulitiseg [3][3]int = [3][3]int{
18+
[3]int{1, 0, 0},
19+
[3]int{0, 1, 0},
20+
[3]int{0, 0, 1}}
21+
22+
//Slice
23+
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
24+
b := a[:]
25+
c := a[3:]
26+
d := a[:6]
27+
e := a[3:6]
28+
29+
fmt.Println(a, " ", b, " ", c, " ", d, " ", e)
30+
31+
32+
33+
fmt.Println(numsstatic[1])
34+
fmt.Println(wordsunspecified[1])
35+
fmt.Println(mulitiseg)
36+
37+
for i := 0; i <= 2; i++ {
38+
fmt.Println(numsstatic[i])
39+
}
40+
41+
42+
//A DYNAMIC ARRAY:
43+
44+
arr := [...]int{1, 2, 3}
45+
fmt.Println(arr)
46+
}
47+

Channels/channle.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
)
7+
8+
var wg = sync.WaitGroup{}
9+
10+
func main() {
11+
//Channel Example
12+
ch := make(chan int)
13+
wg.Add(2) //Add 2 channels
14+
go func() { //Receiving Goroutine
15+
i := <- ch //i is receiving the data sent through ch
16+
fmt.Println(i)
17+
wg.Done()
18+
}()
19+
go func() { //Sending Goroutine
20+
ch <- 42 //The input 42, is being submitted to the channel through "ch" using the syntax "<-"
21+
wg.Done()
22+
}()
23+
wg.Wait()
24+
}

Composition/composition.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 (
4+
"fmt"
5+
)
6+
7+
type Animal struct {
8+
Name string
9+
Origin string
10+
}
11+
12+
type Bird struct {
13+
Animal //Bird is inherating from Animal
14+
SpeedKPH float32
15+
CanFly bool
16+
}
17+
18+
func main() {
19+
b := Bird{
20+
Animal: Animal{
21+
Name: "Emu",
22+
Origin: "Australia",
23+
},
24+
SpeedKPH: 48,
25+
CanFly: false,
26+
}
27+
fmt.Println(b.Name)
28+
}

ConvertToASCII/converttoASCII.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
letter := 'a'
7+
ascii := int(letter)
8+
fmt.Println(ascii)
9+
}

Defer/defer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
fmt.Println("Start")
9+
defer fmt.Println("Middle") //A defer function will run once the main func is complete!
10+
fmt.Println("End")
11+
12+
13+
//A DEFER FUNCTION
14+
defer func() {
15+
if err := recover(); err != nil {
16+
log.Println("Error", err)
17+
}
18+
}()
19+
20+
}
21+
//DEFER PLAYS HERE
22+
23+

Functions/function.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
myFunc("hi")
9+
twoArgsWithSameType("hi", "hello")
10+
11+
//PASSING POINTERS
12+
name := "jeff"
13+
funcWithPointers(&name)
14+
15+
//VARIADIC PARAMETER
16+
sum(1, 2, 3, 4, 5)
17+
18+
//Func with return value
19+
myFuncReturn(2, 6)
20+
21+
//Func returns pointer
22+
myFuncReturnAsPointer(6)
23+
24+
//Result before end
25+
funcResult(7)
26+
27+
//Returns two values
28+
d, err := divide(5.0, 3.0)
29+
if err != nil {
30+
fmt.Println(err)
31+
return //^ Basic error checking ^
32+
}
33+
fmt.Println(d)
34+
35+
36+
//Anonymous Func example
37+
var j int = 7
38+
for i := 0; i <= 5; i++ {
39+
func(j, i int) {
40+
fmt.Println(i + j)
41+
}(i, j)
42+
}
43+
44+
//Anonymous Func as VAR example
45+
f := func(input string) {
46+
fmt.Println(input)
47+
}
48+
49+
f("hi") //Call on func
50+
51+
//Method example
52+
g := greeter {
53+
greeting: "hello",
54+
name: "go",
55+
}
56+
g.greet()
57+
}
58+
59+
func myFunc(msg string) {
60+
fmt.Println(msg)
61+
}
62+
63+
func twoArgsWithSameType(greeting, name string) { //(greeting, name string) NOT (greeting string, name string)
64+
fmt.Println(greeting, name)
65+
}
66+
67+
//Func with pointers
68+
func funcWithPointers(yourName *string) {
69+
*yourName = "John"
70+
fmt.Println(*yourName)
71+
}
72+
73+
//VARIADIC PARAMETER
74+
func sum(values ...int) {
75+
fmt.Println(values)
76+
result := 0
77+
for _, v := range values {
78+
result += v
79+
}
80+
fmt.Println("The sum is: ", result)
81+
}
82+
83+
//Func return value //Return type
84+
func myFuncReturn(num1, num2 int) int{
85+
var result int = num1 + num2
86+
return result
87+
}
88+
89+
//Func return value AS POINTER //Return type
90+
func myFuncReturnAsPointer(num int) *int {
91+
result := num * 2
92+
return &result
93+
}
94+
95+
//Return declaired before using RESULT
96+
func funcResult(num int) (result int) {
97+
result = num
98+
return
99+
}
100+
101+
//This func return 2 values
102+
func divide(a, b float64) (float64, error) {
103+
if b == 0.0 {
104+
return 0.0, fmt.Errorf("Cannot divide by zero")
105+
}
106+
return a / b, nil
107+
}
108+
109+
110+
//Method
111+
type greeter struct {
112+
greeting string
113+
name string
114+
}
115+
116+
func (g greeter) greet() {
117+
fmt.Println(g.greeting, g.name)
118+
}

Goroutines/goroutine.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
)
7+
8+
var wg = sync.WaitGroup{}
9+
10+
func main() {
11+
//Basic Goroutine
12+
var mess = "hello"
13+
wg.Add(1)
14+
go func(msg string) {
15+
fmt.Println(msg)
16+
wg.Done()
17+
}(mess)
18+
mess = "Goodbye"
19+
wg.Wait()
20+
}

Goroutines/multGoroutines.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"sync"
7+
)
8+
9+
var wg = sync.WaitGroup{}
10+
var counter = 0
11+
var m = sync.RWMutex{}
12+
13+
func main() {
14+
runtime.GOMAXPROCS(10) //In this case we are using 10 threads
15+
for i := 0; i <= 10; i++ {
16+
wg.Add(2)
17+
m.RLock() //So its one at a time
18+
go sayHello()
19+
m.Lock() //So it is one at a time
20+
go increment()
21+
}
22+
wg.Wait()
23+
}
24+
25+
func sayHello() {
26+
fmt.Println("Hello", counter)
27+
m.RUnlock()
28+
wg.Done()
29+
}
30+
31+
func increment() {
32+
counter++
33+
m.Unlock()
34+
wg.Done()
35+
}
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
/*package main
55+
56+
import (
57+
"fmt"
58+
"sync"
59+
)
60+
61+
var wg = sync.WaitGroup{}
62+
var counter = 0
63+
64+
func main() {
65+
for i := 0; i <= 10; i++ {
66+
wg.Add(2)
67+
go sayHello()
68+
go increment()
69+
}
70+
wg.Wait()
71+
}
72+
73+
func sayHello() {
74+
fmt.Println("Hello", counter)
75+
wg.Done()
76+
}
77+
78+
func increment() {
79+
counter++
80+
wg.Done()
81+
}
82+
*/

0 commit comments

Comments
 (0)