generated from GirishCodeAlchemy/alchemy-template
-
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
1 parent
dc3b63e
commit b4dc6eb
Showing
16 changed files
with
714 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Package main provides ... | ||
package library | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func sendmessage(Channel chan string) { | ||
fmt.Println("Sending message") | ||
for { | ||
Channel <- "Hello" + time.Now().String() | ||
fmt.Println("message sent @" + time.Now().String()) | ||
time.Sleep(time.Second * 1) | ||
|
||
} | ||
} | ||
|
||
func recivemessage(channel chan string) { | ||
fmt.Println("Reciving message") | ||
for { | ||
fmt.Println("old", <-channel) | ||
} | ||
} | ||
|
||
func SendmessageAck(msg chan string, ack chan string) { | ||
fmt.Println("Sending message") | ||
for { | ||
msg <- "Sent message " + time.Now().String() | ||
time.Sleep(time.Second * 5) | ||
|
||
ackmsg := <-ack | ||
fmt.Println("sentAck -->", ackmsg) | ||
|
||
} | ||
|
||
} | ||
|
||
func RecivemessageAck(msg chan string, ack chan string) { | ||
fmt.Println("Reciving message") | ||
for { | ||
msgRec := <-msg | ||
fmt.Println("receACK -->", msgRec) | ||
ack <- "Reciieved message @" + time.Now().String() | ||
fmt.Println("receACK -->", ack) | ||
} | ||
|
||
} | ||
|
||
func ChannelFunctions() { | ||
channel := make(chan string) | ||
|
||
go sendmessage(channel) | ||
go recivemessage(channel) | ||
|
||
// // Buffered channel | ||
// channel1 := make(chan string, 10) | ||
// go sendmessage(channel1) | ||
// go recivemessage(channel1) | ||
|
||
var stopchannel string | ||
fmt.Scanln(&stopchannel) | ||
msgChan := make(chan string) | ||
ackChan := make(chan string) | ||
go SendmessageAck(msgChan, ackChan) | ||
go RecivemessageAck(msgChan, ackChan) | ||
|
||
fmt.Scanln(&stopchannel) | ||
|
||
} | ||
|
||
// When a channel needs to send a signal of an event without the need for sending any data. From the below piece of code, we can see that we are sending a signal using sending empty struct to the channel which is sent to the workerRoutine. | ||
|
||
func workerRoutine(ch chan struct{}) { | ||
// Receive message from main program. | ||
<-ch | ||
println("Signal Received") | ||
|
||
// Send a message to the main program. | ||
close(ch) | ||
} | ||
|
||
func ChannelWithNodata() { | ||
//Create channel | ||
ch := make(chan struct{}) | ||
|
||
//define workerRoutine | ||
go workerRoutine(ch) | ||
|
||
// Send signal to worker goroutine | ||
ch <- struct{}{} | ||
|
||
// Receive a message from the workerRoutine. | ||
<-ch | ||
println("Signal Received") | ||
|
||
} |
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,63 @@ | ||
package library | ||
|
||
import ( | ||
"container/list" | ||
"fmt" | ||
) | ||
|
||
type entry struct { | ||
key, value int | ||
} | ||
|
||
func display_double_linked_list(list *list.List) { | ||
for e := list.Front(); e != nil; e = e.Next() { | ||
fmt.Printf(" %d -> ", e.Value) | ||
} | ||
fmt.Println() | ||
|
||
// for e := list.Front(); e != nil; e = e.Next() { | ||
// entry := e.Value.(*entry) | ||
// fmt.Printf("%d: %d -> ", entry.key, entry.value) | ||
// } | ||
} | ||
|
||
func insert_before(value int, mark int, curlist *list.List) *list.Element { | ||
fmt.Println("Insert before") | ||
display_double_linked_list(curlist) | ||
var marklist *list.Element | ||
for e := curlist.Front(); e != nil; e = e.Next() { | ||
if e.Value == mark { | ||
marklist = e | ||
break | ||
} | ||
} | ||
|
||
return curlist.InsertBefore(value, marklist) | ||
|
||
// return list.InsertBefore(6, list.Front()) | ||
} | ||
|
||
func double_linked_list() { | ||
fmt.Println("Double Linked List") | ||
|
||
l := list.New() | ||
|
||
l.PushBack(1) | ||
l.PushBack(2) | ||
l.PushBack(3) | ||
display_double_linked_list(l) | ||
|
||
l.PushBack(4) | ||
l.PushFront(5) | ||
display_double_linked_list(l) | ||
|
||
l.InsertBefore(6, l.Back()) | ||
l.InsertBefore(6, l.Front()) | ||
insert_before(6, 2, l) | ||
display_double_linked_list(l) | ||
|
||
} | ||
|
||
func init() { | ||
double_linked_list() | ||
} |
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 library | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"math" | ||
) | ||
|
||
func sqrt(x float64) (float64, error) { | ||
if x < 0 { | ||
return 0, errors.New("Math: negative number passed to sort") | ||
// return 0, fmt.Errorf("Math: negative number passed to sort: %v", x) | ||
} | ||
return math.Sqrt(x), nil | ||
} | ||
|
||
func ErrorCase() { | ||
res, err := sqrt(-1) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
fmt.Println(res) | ||
} |
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,43 @@ | ||
package library | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
) | ||
|
||
// RangeForLoop ... | ||
func RangeForLoop() { | ||
// pow := []int{1, 2, 4, 8, 16, 32, 64, 128} | ||
pow0 := []int{0, 1, 2, 3, 4, 5, 6} | ||
for i, v := range pow0 { | ||
fmt.Printf("2**%d = %d\n", i, int(math.Pow(2, float64(v)))) | ||
} | ||
|
||
pow := make([]int, 10) | ||
for i := range pow { | ||
pow[i] = 1 << uint(i) // == 2**i | ||
} | ||
for _, value := range pow { | ||
fmt.Printf("%d\n", value) | ||
} | ||
} | ||
func ForLoopIterator() { | ||
for i := 0; i < 10; i++ { | ||
fmt.Println(i) | ||
} | ||
} | ||
|
||
func WhileForLoop() { | ||
var ctr int = 0 | ||
for ctr < 5 { | ||
fmt.Println(ctr) | ||
ctr++ | ||
} | ||
} | ||
|
||
func init() { | ||
fmt.Println("\n******** For Loop smaple code *********") | ||
ForLoopIterator() | ||
RangeForLoop() | ||
WhileForLoop() | ||
} |
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 library | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
func init() { | ||
fmt.Println("\n******** Get Key maps *********") | ||
|
||
a := map[string]int{ | ||
"A": 1, "B": 2, | ||
} | ||
keys := reflect.ValueOf(a).MapKeys() | ||
strkeys := make([]string, len(keys)) | ||
for i := 0; i < len(keys); i++ { | ||
strkeys[i] = keys[i].String() | ||
} | ||
fmt.Println(strkeys) | ||
fmt.Print(strings.Join(strkeys, ",")) | ||
} |
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,24 @@ | ||
package library | ||
|
||
import "fmt" | ||
|
||
func add(a int, b int) { | ||
fmt.Println("---->", a, b, "-->", a+b) | ||
} | ||
|
||
func GoroutinesFunction() { | ||
for i := 0; i < 10; i++ { | ||
go add(i, i) | ||
} | ||
fmt.Println("---->done") | ||
add(30, 20) | ||
var abc string | ||
|
||
go func() { | ||
fmt.Println("Annonynmous goroutine") | ||
}() | ||
|
||
fmt.Scanln(&abc) | ||
fmt.Println("---->", abc) | ||
fmt.Println("--->") | ||
} |
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,111 @@ | ||
package library | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
) | ||
|
||
type Human interface { | ||
Display() | ||
} | ||
|
||
type Person struct { | ||
Name string | ||
Age int | ||
} | ||
|
||
func (p Person) Display() { | ||
fmt.Println("Name:", p.Name, "Age:", p.Age) | ||
} | ||
|
||
func InterfaceFunction() { | ||
var h Human | ||
p := Person{ | ||
Name: "John", | ||
Age: 25, | ||
} | ||
h = p | ||
h.Display() | ||
fmt.Println(h) | ||
|
||
var h1 Human = Person{Name: "John", Age: 25} | ||
h1.Display() | ||
|
||
circle := Circle{x: 0, y: 0, radius: 5} | ||
rectangle := Rectangle{width: 10, height: 5} | ||
|
||
fmt.Println("Area of circle: ", getArea(circle)) | ||
fmt.Println("Area of Rectanlge: ", getArea(rectangle)) | ||
|
||
nodepod := NodePods{name: "node", version: "v10", npm: "npm"} | ||
pythonpod := PythonPods{name: "python", version: "v3.8", pip: "pip"} | ||
fmt.Println(getPods(nodepod)) | ||
fmt.Println(getPods(pythonpod)) | ||
getAllPods(nodepod, pythonpod) | ||
} | ||
|
||
// // ########################## Shape #################################### | ||
|
||
type Shape interface { | ||
area() float64 | ||
} | ||
|
||
/* define a circle */ | ||
type Circle struct { | ||
x, y, radius float64 | ||
} | ||
|
||
/* define a rectangle */ | ||
type Rectangle struct { | ||
width, height float64 | ||
} | ||
|
||
/* define a method for circle (implementation of Shape.area())*/ | ||
func (circle Circle) area() float64 { | ||
return math.Pi * circle.radius * circle.radius | ||
} | ||
|
||
/* define a method for rectangle (implementation of Shape.area())*/ | ||
func (rect Rectangle) area() float64 { | ||
return rect.width * rect.height | ||
} | ||
|
||
func getArea(shape Shape) float64 { | ||
return shape.area() | ||
} | ||
|
||
// ########################## Pods #################################### | ||
|
||
type Pods interface { | ||
getPods() string | ||
} | ||
|
||
type NodePods struct { | ||
name string | ||
version string | ||
npm string | ||
} | ||
|
||
type PythonPods struct { | ||
name string | ||
version string | ||
pip string | ||
} | ||
|
||
func (np NodePods) getPods() string { | ||
return "NodePods " + np.name + " " + np.version + " " + np.npm | ||
} | ||
|
||
func (pp PythonPods) getPods() string { | ||
return "PythonPods " + pp.name + " " + pp.version + " " + pp.pip | ||
} | ||
|
||
func getPods(pod Pods) string { | ||
return pod.getPods() | ||
} | ||
|
||
func getAllPods(pods ...Pods) { | ||
for _, pod := range pods { | ||
fmt.Println(pod.getPods()) | ||
} | ||
} |
Oops, something went wrong.