Skip to content

Commit d07b61b

Browse files
Go/Beginner-8uiZC0l4Ajw
1 parent 960acf5 commit d07b61b

File tree

14 files changed

+429
-0
lines changed

14 files changed

+429
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("Hello, World!")
7+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var intSlice = []int{1, 2, 3}
7+
fmt.Println(sumSlice[int](intSlice))
8+
9+
var float32Slice = []float32{1, 2, 3}
10+
fmt.Println(sumSlice[float32](float32Slice))
11+
}
12+
13+
func sumSlice[T int | float32 | float64](slice []T) T {
14+
var sum T
15+
for _, v := range slice {
16+
sum += v
17+
}
18+
return sum
19+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"unicode/utf8"
6+
)
7+
8+
// uint = only positive integer
9+
10+
func main(){
11+
var intNum uint = 32767;
12+
intNum = intNum + 1;
13+
fmt.Println(intNum);
14+
// var floatNum float64;
15+
// fmt.Println(floatNum)
16+
var floatNum float32 = 10.1;
17+
var intNum32 int32 = 2;
18+
var result float32 = floatNum + float32(intNum32)
19+
fmt.Println(result)
20+
21+
var intNum1 int = 3
22+
var intNum2 int = 2
23+
fmt.Println(intNum1/intNum2)
24+
fmt.Println(intNum1%intNum2)
25+
26+
var myString string = "Hello World"
27+
fmt.Println(myString)
28+
29+
var testString = `
30+
Multi Line Strings type shi
31+
`
32+
fmt.Println(testString)
33+
34+
/*
35+
so here we have to do this since when we call the len() function in go it provides us the number of bytes
36+
(and in scenario where we get weird aaa characters then it does not exactly work per say yk?)
37+
*/
38+
fmt.Println(utf8.RuneCountInString("weird sigma symbol type shi"))
39+
40+
var myRune rune = 'a';
41+
fmt.Println(myRune)
42+
43+
var myBoolean bool = false
44+
fmt.Println(myBoolean)
45+
46+
var intNum3 int
47+
fmt.Println(intNum3)
48+
49+
// var myVar = "text"
50+
// same as:
51+
myVar := "text"
52+
fmt.Println(myVar)
53+
54+
var var1, var2 = 1, 2
55+
fmt.Println(var1, var2)
56+
57+
const myConst string = "const value"
58+
fmt.Println(myConst)
59+
60+
const pi float32 = 3.1415
61+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
var printValue string = "Hello Word"
10+
printMe(printValue)
11+
12+
var numerator int = 11
13+
var denominator int = 2
14+
var result, remainder, err = intDivision(numerator, denominator)
15+
// if err!= nil {
16+
// fmt.Println(err.Error())
17+
// } else if remainder == 0{
18+
// fmt.Printf("the result of the integer division is %v", result)
19+
// } else {
20+
// fmt.Printf("The result of the integer divison is %v and the remainder is %v", result, remainder)
21+
// }
22+
switch {
23+
case err != nil:
24+
fmt.Printf(err.Error())
25+
case remainder == 0:
26+
fmt.Printf("the result of the intger divison is %v", result)
27+
default:
28+
fmt.Printf("the result of the integer division is %v with remainder %v", result, remainder)
29+
}
30+
31+
switch remainder {
32+
case 0:
33+
fmt.Printf("the division was exact")
34+
case 1, 2:
35+
fmt.Printf("the division was close")
36+
default:
37+
fmt.Printf("the division was not close")
38+
}
39+
}
40+
41+
func printMe(printValue string) {
42+
fmt.Println(printValue)
43+
}
44+
45+
func intDivision(numerator int, denominator int) (int, int, error) {
46+
var err error
47+
if denominator == 0 {
48+
err = errors.New("cannot divide by zero")
49+
return 0, 0, err
50+
}
51+
var result int = numerator / denominator
52+
var remainder int = numerator % denominator
53+
return result, remainder, err
54+
}
55+
56+
// if 1==1 && or || 2==2
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var intArr [3]int32
7+
intArr[1] = 123
8+
fmt.Println(intArr[0])
9+
10+
// Continuous Memory, So that each of the elements are next to each other so that we know the locations yk?
11+
fmt.Println(&intArr[0])
12+
13+
// var intArry [3]int32 = [3]int32{1, 2, 3}
14+
intArry := [...]int32{1, 2, 3}
15+
fmt.Println(intArry)
16+
17+
var intSlice []int32 = []int32{4, 5, 6}
18+
fmt.Println(intSlice)
19+
intSlice = append(intSlice, 7)
20+
fmt.Println(intSlice)
21+
22+
var intSlice2 []int32 = []int32{8, 9}
23+
intSlice = append(intSlice, intSlice2...)
24+
fmt.Println(intSlice)
25+
26+
var intSlice3 []int32 = make([]int32, 3, 8)
27+
fmt.Println(intSlice3)
28+
29+
var myMap map[string]uint8 = make(map[string]uint8)
30+
fmt.Println(myMap)
31+
32+
var myMap2 = map[string]uint8{"Adam": 23, "Sarah": 45}
33+
fmt.Println(myMap2["Adam"])
34+
var age, ageExist = myMap2["Jason"]
35+
// delete(myMap2, "Adam")
36+
if ageExist {
37+
fmt.Printf("the age is %v", age)
38+
} else {
39+
fmt.Println("invalid key brh1")
40+
}
41+
42+
for name, age := range myMap2 {
43+
fmt.Printf("Name %v, age %v", name, age)
44+
}
45+
46+
for i, v := range intArr {
47+
fmt.Printf("Index %v, Value %v", i, v)
48+
}
49+
var i int = 0
50+
// for i > 10 {
51+
// fmt.Println(i)
52+
// i += 1
53+
// }
54+
for {
55+
if i > 10 {
56+
break
57+
}
58+
fmt.Println(i)
59+
i += 1
60+
}
61+
for i := 0; i < 10; i++ {
62+
fmt.Println(i)
63+
}
64+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func main() {
9+
var myString = "résumé"
10+
var myRunes = []rune("résumé")
11+
var indexed = myString[0]
12+
fmt.Println(myRunes)
13+
fmt.Println(indexed)
14+
fmt.Println(myString)
15+
16+
var strSlice = []string{"s", "u", "b", "s", "c", "r", "i", "b", "e"}
17+
var strBuilder strings.Builder
18+
for i := range strSlice {
19+
strBuilder.WriteString(strSlice[i])
20+
}
21+
var catStr = strBuilder.String()
22+
fmt.Printf("%v", catStr)
23+
}
24+
25+
/*
26+
so in fmt.Printf()
27+
28+
we can use %T to get the type of a specific value
29+
30+
var value int = 1;
31+
fmt.Printf("%v %T", value, value)
32+
*/
33+
34+
/*
35+
so in the string setup its a bit complicated ig but not that bad, here is how it goes
36+
37+
golang uses utf 8 which is a dynamic byte setup where a specific character might take up 8 or 16 or continued no. of bits, we dont really know
38+
so in turn it is really hard for us to manage it right, so because of that when we index for example we get the uint no. of that specific byte, but the thing is 1 character can have more than 1 byte so its a bit of a tricky situation, you can go through the for loop and get it as well but still too complicated.
39+
40+
the easiest way it seems is to pretty much just use runes since they will auto calculate the uint of a character since its represented individually
41+
*/
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type gasEngine struct {
6+
mpg uint8
7+
gallons uint8
8+
}
9+
type electricEngine struct {
10+
mpkwh uint8
11+
kwh uint8
12+
}
13+
14+
func (e gasEngine) milesLeft() uint8 {
15+
return e.gallons * e.mpg
16+
}
17+
func (e electricEngine) milesLeft() uint8 {
18+
return e.kwh * e.mpkwh
19+
}
20+
21+
type engine interface {
22+
milesLeft() uint8
23+
}
24+
25+
func canMakeIt(e engine, miles uint8) bool {
26+
return miles <= e.milesLeft()
27+
}
28+
func main() {
29+
var myEngine gasEngine = gasEngine{mpg: 25, gallons: 15}
30+
myEngine.mpg = 28
31+
fmt.Println(myEngine)
32+
fmt.Printf("total miles left %v \n", myEngine.milesLeft())
33+
fmt.Printf("chat, can i make it? %v", canMakeIt(myEngine, 10))
34+
}
35+
36+
/*
37+
type gasEngine struct {
38+
mpg uint8
39+
gallons uint8
40+
owner owner
41+
}
42+
this will have like
43+
{
44+
mpg: smthn
45+
gallons: smthn
46+
owner: {
47+
name: "Alex"
48+
}
49+
}
50+
51+
52+
type gasEngine struct {
53+
mpg uint8
54+
gallons uint8
55+
owner
56+
}
57+
this will have like{
58+
mpg: smthn
59+
gallons: smthn
60+
name: "Alex"
61+
}
62+
*/
63+
64+
/*
65+
var myEngine2 = struct {
66+
mpg uint8
67+
gallons uint8
68+
}{25, 15}
69+
// the myEngine2 is a way to create a temp struct but that specific structure cant really be reused per say yk?
70+
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// func main() {
6+
// var p *int32 = new(int32)
7+
// var i int32
8+
// fmt.Printf("the value p points to is: %v", *p)
9+
// fmt.Printf("the value if i is: %v", i)
10+
// *p = 10 // so when we do new() have a memory location we point to right, we update the memory within that, the variable p still refers to the original memory location which has the memory location of the new() variable
11+
// p = &i // now p and i both refer to the same spot in memory
12+
// *p = 1
13+
// }
14+
func main() {
15+
var thing1 = [5]float64{1, 2, 3, 4, 5}
16+
fmt.Printf("\n the memory location of the thing1 array is: %p", &thing1)
17+
var result [5]float64 = square(&thing1)
18+
fmt.Printf("\n the result is: %v", result)
19+
fmt.Printf("\n the value of thing1 is: %v", thing1)
20+
}
21+
22+
func square(thing2 *[5]float64) [5]float64 {
23+
for i := range thing2 {
24+
thing2[i] = thing2[i] * thing2[i]
25+
}
26+
return *thing2
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
"time"
7+
)
8+
9+
// RWMutex is specifically for Reading and Writing, Mutex is just general purpose, and this provides functions like RLock(), etc...
10+
var m = sync.RWMutex{}
11+
var wg = sync.WaitGroup{}
12+
var dbData = []string{"id1", "id2", "id3", "id4", "id5"}
13+
var results = []string{}
14+
15+
func main() {
16+
t0 := time.Now()
17+
for i := 0; i < len(dbData); i++ {
18+
wg.Add(1)
19+
go dbCall(i)
20+
}
21+
wg.Wait()
22+
fmt.Printf("total execution time: %v", time.Since(t0))
23+
}
24+
func dbCall(i int) {
25+
var delay float32 = 2000
26+
time.Sleep(time.Duration(delay) * time.Millisecond)
27+
var result string = dbData[i]
28+
save(result)
29+
log()
30+
wg.Done()
31+
}
32+
33+
func save(result string) {
34+
m.Lock()
35+
results = append(results, result)
36+
m.Unlock()
37+
}
38+
39+
func log() {
40+
m.RLock()
41+
fmt.Println("the results from the database is: ", results)
42+
m.RUnlock()
43+
}

0 commit comments

Comments
 (0)