Skip to content

Commit 175c383

Browse files
init
0 parents  commit 175c383

31 files changed

+587
-0
lines changed

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/Golang-Learn.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/golinter.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/material_theme_project_new.xml

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/templateLanguages.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

channels/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/charithmadhuranga/channels
2+
3+
go 1.23.6

channels/helpers/helper.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package helpers
2+
3+
import (
4+
"math/rand"
5+
)
6+
7+
func RandomNumber(n int) int {
8+
value := rand.Intn(n)
9+
return value
10+
}

channels/main.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/charithmadhuranga/channels/helpers"
6+
)
7+
8+
const numpool = 1000
9+
10+
func CalculateValue(intChan chan int) {
11+
randomNum := helpers.RandomNumber(numpool)
12+
intChan <- randomNum
13+
14+
}
15+
func main() {
16+
intChan := make(chan int)
17+
defer close(intChan)
18+
go CalculateValue(intChan)
19+
num := <-intChan
20+
fmt.Println(num)
21+
}

conditions.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
)
7+
8+
func main() {
9+
var myVar int
10+
var myVar2 string
11+
12+
fmt.Println("Type any number ?")
13+
_, _ = fmt.Scan(&myVar)
14+
fmt.Println("Type any animal name ?")
15+
_, _ = fmt.Scan(&myVar2)
16+
17+
if myVar < 100 && myVar2 == "cat" {
18+
log.Println("Variable is less than 100 and other variable is equal to cat")
19+
} else if myVar > 100 && myVar2 == "cat" {
20+
log.Println("yes variable is greater than 100 and other variable is equal to cat")
21+
} else {
22+
log.Println("I don't know what to do anything")
23+
}
24+
25+
switch myVar {
26+
case 100:
27+
log.Println("Variable is equal to 100")
28+
case 200:
29+
log.Println("Variable is equal to 200")
30+
case 300:
31+
log.Println("Variable is equal to 300")
32+
case 400:
33+
log.Println("Variable is equal to 400")
34+
case 500:
35+
log.Println("Variable is equal to 500")
36+
default:
37+
log.Println("Variable must be bigger than 500 or less than 100")
38+
39+
}
40+
41+
}

interfaces.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type Animal interface {
6+
Says() string
7+
NumberOfLegs() int
8+
}
9+
10+
type Dog struct {
11+
Name string
12+
Breed string
13+
}
14+
type Gorilla struct {
15+
Name string
16+
Color string
17+
NumberOfTeeth int
18+
}
19+
20+
func main() {
21+
22+
dog := Dog{
23+
Name: "Samantha",
24+
Breed: "GermanShepard",
25+
}
26+
PrintInfo(&dog)
27+
28+
goriilla := Gorilla{
29+
Name: "Jock",
30+
Color: "red",
31+
NumberOfTeeth: 38,
32+
}
33+
PrintInfo(&goriilla)
34+
35+
}
36+
37+
func PrintInfo(a Animal) {
38+
fmt.Println("This animal says", a.Says(), "and has", a.NumberOfLegs(), "legs")
39+
}
40+
41+
func (d *Dog) Says() string {
42+
return "Woof"
43+
}
44+
func (d *Dog) NumberOfLegs() int {
45+
return 4
46+
47+
}
48+
49+
func (d *Gorilla) Says() string {
50+
return "Goo"
51+
}
52+
func (d *Gorilla) NumberOfLegs() int {
53+
return 2
54+
55+
}

json/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.come/charithmadhuranga/jsonhandle
2+
3+
go 1.23.6

json/main.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
)
8+
9+
type Person struct {
10+
FirstName string `json:"first_name"`
11+
LastName string `json:"last_name"`
12+
HairColor string `json:"hair_color"`
13+
HasDog bool `json:"has_dog"`
14+
}
15+
16+
func main() {
17+
myJson := `
18+
[
19+
{
20+
"first_name": "John",
21+
"last_name": "Doe",
22+
"hair_color": "blue",
23+
"has_dog" : true
24+
},
25+
{
26+
"first_name": "Jane",
27+
"last_name": "Doe",
28+
"hair_color": "black",
29+
"has_dog" : false
30+
}
31+
]`
32+
33+
var unmarshaled []Person
34+
err := json.Unmarshal([]byte(myJson), &unmarshaled)
35+
if err != nil {
36+
log.Println("Error unmashalling json", err)
37+
}
38+
log.Printf("unmarshaled: %v\n", unmarshaled)
39+
40+
var mySlice []Person
41+
var m1 Person
42+
m1.FirstName = "Gane"
43+
m1.LastName = "Moe"
44+
m1.HairColor = "Red"
45+
m1.HasDog = true
46+
47+
mySlice = append(mySlice, m1)
48+
49+
var m2 Person
50+
m2.FirstName = "Wolly"
51+
m2.LastName = "Dane"
52+
m2.HairColor = "Reder"
53+
m2.HasDog = false
54+
55+
mySlice = append(mySlice, m2)
56+
57+
newJson, err := json.MarshalIndent(mySlice, "", " ")
58+
if err != nil {
59+
log.Println("Error marshaling json", err)
60+
}
61+
fmt.Println(string(newJson))
62+
}

loop.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
animals := []string{"dog", "fish", "horse", "mouse", "cat"}
9+
10+
for _, animal := range animals {
11+
fmt.Println(animal)
12+
}
13+
14+
mapAnimals := map[string]string{
15+
"dog": "Fido",
16+
"fish": "Dummy",
17+
}
18+
for animalType, animal := range mapAnimals {
19+
fmt.Println(animalType, animal)
20+
}
21+
var firstLine = "First line is a Supper long"
22+
23+
for i, line := range firstLine {
24+
fmt.Println(i, line)
25+
}
26+
27+
type User struct {
28+
FirstName string
29+
LastName string
30+
Email string
31+
Age int
32+
}
33+
var users []User
34+
users = append(users, User{"John", "Doe", "[email protected]", 20})
35+
users = append(users, User{"Jane", "Doe", "[email protected]", 20})
36+
users = append(users, User{"Charlie", "Doe", "[email protected]", 20})
37+
38+
for _, user := range users {
39+
fmt.Println(user.FirstName, user.LastName, user.Email, user.Age)
40+
}
41+
42+
}

main.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("hello world")
7+
var whatToSay string
8+
var i int
9+
10+
whatToSay = "Hello, Cruel world"
11+
fmt.Println(whatToSay)
12+
13+
i = 24
14+
fmt.Println("i is set to", i)
15+
16+
whatwasSaaid, theotherThing := saySomething()
17+
fmt.Println("The funciton returned", whatwasSaaid)
18+
fmt.Println("The funciton other returned", theotherThing)
19+
}
20+
21+
func saySomething() (string, string) {
22+
return "Hello World", "Second thing"
23+
}

maps.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
)
7+
8+
type Dogs struct {
9+
Japanese string
10+
Korean string
11+
}
12+
13+
func main() {
14+
myMap := map[string]string{
15+
"first": "John",
16+
"last": "Doe",
17+
}
18+
otherMap := make(map[string]string)
19+
20+
Doggie := make(map[string]Dogs)
21+
22+
BillaDoggie := Dogs{
23+
Japanese: "Japan",
24+
Korean: "Korean",
25+
}
26+
27+
Doggie["Bill"] = BillaDoggie
28+
29+
otherMap["first"] = "Jane"
30+
fmt.Println(myMap)
31+
fmt.Println(otherMap)
32+
log.Println(otherMap["first"])
33+
log.Println(otherMap["last"])
34+
log.Println(Doggie["Bill"].Korean)
35+
36+
}

packages/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/charithmadhuranga/mynice
2+
3+
go 1.23.6

packages/helpers/helpers.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package helpers
2+
3+
type SomeType struct {
4+
TypeName string
5+
TypeNum int
6+
}

packages/main.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"github.com/charithmadhuranga/mynice/helpers"
5+
"log"
6+
)
7+
8+
func main() {
9+
10+
var myVar helpers.SomeType
11+
myVar.TypeName = "SomeType"
12+
log.Println(myVar.TypeName)
13+
14+
}

0 commit comments

Comments
 (0)