forked from shijuvar/gokit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
709 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
# Folders | ||
_obj | ||
_test | ||
.vscode | ||
|
||
# Architecture specific extensions/prefixes | ||
*.[568vq] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
// Declare arrays | ||
var x [5]int | ||
// Assign values at specific index | ||
x[0] = 5 | ||
x[4] = 25 | ||
fmt.Println("Value of x:", x) | ||
|
||
x[1] = 10 | ||
x[2] = 15 | ||
x[3] = 20 | ||
fmt.Println("Value of x:", x) | ||
|
||
// Declare and initialize array with array literal | ||
y := [5]int{10, 20, 30, 40, 50} | ||
fmt.Println("Value of y:", y) | ||
fmt.Println("Length of y:", len(y)) | ||
|
||
// Array literal with ... | ||
z := [...]int{10, 20, 30, 40, 50} | ||
fmt.Println("Value of z:", z) | ||
fmt.Println("Length of z:", len(z)) | ||
|
||
// Initialize values at specific index with array literal | ||
langs := [4]string{0: "Go", 3: "Julia"} | ||
fmt.Println("Value of langs:", langs) | ||
// Assign values to remain positions | ||
langs[1] = "Rust" | ||
langs[2] = "Scala" | ||
|
||
// Iterate over the elements of array | ||
fmt.Println("Value of langs:", langs) | ||
fmt.Println("\nIterate over arrays\n") | ||
for i := 0; i < len(langs); i++ { | ||
fmt.Printf("langs[%d]:%s \n", i, langs[i]) | ||
} | ||
fmt.Println("\n") | ||
|
||
// Iterate over the elements of array using range | ||
for k, v := range langs { | ||
fmt.Printf("langs[%d]:%s \n", k, v) | ||
} | ||
for k := range langs { | ||
fmt.Printf("Index:%d \n", k) | ||
} | ||
for _, v := range langs { | ||
fmt.Printf("Value:%s \n", v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
func ReadFile(filename string) ([]byte, error) { | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
return ioutil.ReadAll(f) | ||
} | ||
|
||
func main() { | ||
f, _ := ReadFile("test.txt") | ||
fmt.Println("%s", f) | ||
fmt.Println(string(f)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func panicRecover() { | ||
|
||
defer fmt.Println("Deferred call - 1") | ||
defer func() { | ||
fmt.Println("Deferred call - 2") | ||
if e := recover(); e != nil { | ||
// e is the value passed to panic() | ||
fmt.Println("Recover with: ", e) | ||
} | ||
}() | ||
panic("Just panicking for the sake of example") | ||
fmt.Println("This will never be called") | ||
} | ||
|
||
func main() { | ||
fmt.Println("Starting to panic") | ||
panicRecover() | ||
fmt.Println("Program regains control after the panic recovery") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
this is a sample text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Example program with Interface, Composition and Method Overriding | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type TeamMember interface { | ||
PrintName() | ||
PrintDetails() | ||
} | ||
|
||
type Employee struct { | ||
FirstName, LastName string | ||
Dob time.Time | ||
JobTitle, Location string | ||
} | ||
|
||
func (e Employee) PrintName() { | ||
fmt.Printf("\n%s %s\n", e.FirstName, e.LastName) | ||
} | ||
|
||
func (e Employee) PrintDetails() { | ||
fmt.Printf("Date of Birth: %s, Job: %s, Location: %s\n", e.Dob.String(), e.JobTitle, e.Location) | ||
} | ||
|
||
type Developer struct { | ||
Employee //type embedding for composition | ||
Skills []string | ||
} | ||
|
||
// Overrides the PrintDetails | ||
func (d Developer) PrintDetails() { | ||
// Call Employee PrintDetails | ||
d.Employee.PrintDetails() | ||
fmt.Println("Technical Skills:") | ||
for _, v := range d.Skills { | ||
fmt.Println(v) | ||
} | ||
} | ||
|
||
type Manager struct { | ||
Employee //type embedding for composition | ||
Projects []string | ||
Locations []string | ||
} | ||
|
||
// Overrides the PrintDetails | ||
func (m Manager) PrintDetails() { | ||
// Call Employee PrintDetails | ||
m.Employee.PrintDetails() | ||
fmt.Println("Projects:") | ||
for _, v := range m.Projects { | ||
fmt.Println(v) | ||
} | ||
fmt.Println("Managing teams for the locations:") | ||
for _, v := range m.Locations { | ||
fmt.Println(v) | ||
} | ||
} | ||
|
||
type Team struct { | ||
Name, Description string | ||
TeamMembers []TeamMember | ||
} | ||
|
||
func (t Team) PrintTeamDetails() { | ||
fmt.Printf("Team: %s - %s\n", t.Name, t.Description) | ||
fmt.Println("Details of the team members:") | ||
for _, v := range t.TeamMembers { | ||
v.PrintName() | ||
v.PrintDetails() | ||
} | ||
} | ||
|
||
func main() { | ||
steve := Developer{ | ||
Employee: Employee{ | ||
FirstName: "Steve", | ||
LastName: "John", | ||
Dob: time.Date(1990, time.February, 17, 0, 0, 0, 0, time.UTC), | ||
JobTitle: "Software Engineer", | ||
Location: "San Fancisco", | ||
}, | ||
Skills: []string{"Go", "Docker", "Kubernetes"}, | ||
} | ||
irene := Developer{ | ||
Employee: Employee{ | ||
FirstName: "Irene", | ||
LastName: "Rose", | ||
Dob: time.Date(1991, time.January, 13, 0, 0, 0, 0, time.UTC), | ||
JobTitle: "Software Engineer", | ||
Location: "Santa Clara", | ||
}, | ||
Skills: []string{"Go", "MongoDB"}, | ||
} | ||
alex := Manager{ | ||
Employee: Employee{ | ||
FirstName: "Alex", | ||
LastName: "Williams", | ||
Dob: time.Date(1979, time.February, 17, 0, 0, 0, 0, time.UTC), | ||
JobTitle: "Program Manger", | ||
Location: "Santa Clara", | ||
}, | ||
Projects: []string{"CRM", "e-Commerce"}, | ||
Locations: []string{"San Fancisco", "Santa Clara"}, | ||
} | ||
|
||
// Create team | ||
team := Team{ | ||
"Go", | ||
"Golang Engineering Team", | ||
[]TeamMember{steve, irene, alex}, | ||
} | ||
// Get details of Team | ||
team.PrintTeamDetails() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func Add(x, y int) int { | ||
return x + y | ||
} | ||
|
||
func Subtract(x, y int) int { | ||
return x - y | ||
} | ||
|
||
func main() { | ||
x, y := 20, 10 | ||
|
||
result := Add(x, y) | ||
fmt.Println("[Add]:", result) | ||
|
||
result = Subtract(x, y) | ||
fmt.Println("[Subtract]:", result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func SplitValues(f func(sum int) (int, int)) { | ||
x, y := f(35) | ||
fmt.Println(x, y) | ||
|
||
x, y = f(50) | ||
fmt.Println(x, y) | ||
} | ||
|
||
func main() { | ||
a, b := 5, 8 | ||
fn := func(sum int) (int, int) { | ||
x := sum * a / b | ||
y := sum - x | ||
return x, y | ||
} | ||
|
||
// Passing function value as an argument to another function | ||
SplitValues(fn) | ||
|
||
// Calling the function value by providing argument | ||
x, y := fn(20) | ||
fmt.Println(x, y) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func Swap(x, y string) (string, string) { | ||
return y, x | ||
} | ||
|
||
func main() { | ||
x, y := "Shiju", "Varghese" | ||
fmt.Println("Before Swap:", x, y) | ||
|
||
x, y = Swap(x, y) | ||
fmt.Println("After Swap:", x, y) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func Sum(nums ...int) int { | ||
total := 0 | ||
for _, num := range nums { | ||
total += num | ||
} | ||
return total | ||
} | ||
|
||
func main() { | ||
// Providing four arguments | ||
total := Sum(1, 2, 3, 4) | ||
fmt.Println("The Sum is:", total) | ||
|
||
// Providing three arguments | ||
total = Sum(5, 7, 8) | ||
fmt.Println("The Sum is:", total) | ||
|
||
// Providing a Slice as an argument | ||
nums := []int{1, 2, 3, 4, 5} | ||
total = Sum(nums...) | ||
fmt.Println("The Sum is:", total) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
// Declares a nil map | ||
var chapts map[int]string | ||
|
||
// Initialize map with make function | ||
chapts = make(map[int]string) | ||
|
||
// Add data as key/value pairs | ||
chapts[1] = "Beginning Go" | ||
chapts[2] = "Go Fundamentals" | ||
chapts[3] = "Structs and Interfaces" | ||
|
||
// Iterate over the elements of map using range | ||
for k, v := range chapts { | ||
fmt.Printf("Key: %d Value: %s\n", k, v) | ||
} | ||
|
||
// Declare and initialize map using map literal | ||
langs := map[string]string{ | ||
"EL": "Greek", | ||
"EN": "English", | ||
"ES": "Spanish", | ||
"FR": "French", | ||
"HI": "Hindi", | ||
} | ||
|
||
// Delete an element | ||
delete(langs, "EL") | ||
|
||
// Lookout an element with key | ||
if lan, ok := langs["EL"]; ok { | ||
fmt.Println(lan) | ||
} else { | ||
fmt.Println("\nKey doesn't exists") | ||
} | ||
// Passing a map to function doesn't make a copy | ||
removeLan(langs, "HI") | ||
for k, v := range langs { | ||
fmt.Printf("Key: %s Value: %s\n", k, v) | ||
} | ||
} | ||
func removeLan(langs map[string]string, key string) { | ||
delete(langs, key) | ||
} |
Oops, something went wrong.