Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
0427a87
add practice of go:getting started course
JayPonda Feb 13, 2023
2921901
move course dir into golang dir
JayPonda Feb 13, 2023
144bb5b
both dir modified
JayPonda Feb 13, 2023
11a7fc9
add: input example in module1| module2 and module3
JayPonda Feb 14, 2023
dc7f2f0
add: time practice in module4
JayPonda Feb 14, 2023
7acdb67
add: module4 for string operation
JayPonda Feb 14, 2023
ede8265
adding new folder concurrent_programming_with_go
JayPonda Feb 15, 2023
66f826f
add: module4 pf string opr with module5
JayPonda Feb 15, 2023
f8aac69
add: practice of reflection
JayPonda Feb 15, 2023
2d2eaa8
add: create types / functions at runtime
JayPonda Feb 16, 2023
b03f32a
add: practise of channels
JayPonda Feb 17, 2023
0272680
get github changes in local may have conflict
JayPonda Feb 17, 2023
59f6b67
add: gitignore
JayPonda Feb 17, 2023
f2df9a0
add: program which simulates try-catch
JayPonda Feb 17, 2023
86718bb
add: gitignore
JayPonda Feb 17, 2023
b076291
delete .gitignore in go_getting_started
JayPonda Feb 17, 2023
424aa46
CHanged
devarshi-007 Feb 17, 2023
8b877f2
Delete Devarshi_Trivedi directory
devarshi-007 Feb 17, 2023
bbeac92
Delete README.md
devarshi-007 Feb 17, 2023
3be6665
add: testing_go_applications
JayPonda Feb 17, 2023
8fb138e
add: README.md
JayPonda Feb 17, 2023
fca1663
Merge branch 'addedGitIgnore' into jay_version
JayPonda Feb 17, 2023
2c3127f
add: creating web services with go
JayPonda Feb 17, 2023
5665560
Merge branch 'updatebranch' into jay_version
JayPonda Feb 17, 2023
4a28f74
add|rename: add benchmark_test.go
JayPonda Feb 17, 2023
0c06097
add: crud operation with http
JayPonda Feb 21, 2023
2d87681
add: delete method in productHandler
Jay-ponda-improwised Feb 23, 2023
0c768f2
add: new folder as replacement of database simulation
Jay-ponda-improwised Feb 25, 2023
c064ab5
apply: context to database connection
Jay-ponda-improwised Feb 25, 2023
2947897
Create: new project concurrency_project
Jay-ponda-improwised Feb 25, 2023
a8cc3f8
Merge branch 'jay_version' of github.com-improwised:devarshi-007/trai…
Jay-ponda-improwised Feb 25, 2023
7140232
add: file upload-download
JayPonda Feb 27, 2023
765c2de
complate: socket demo
JayPonda Mar 11, 2023
f0b95ed
complate: template demo
JayPonda Mar 11, 2023
9786481
delete: project folder
JayPonda Mar 12, 2023
96bc858
add httpAsClient
Jay-ponda-improwised Mar 30, 2023
fcf89c8
remove vendor
Jay-ponda-improwised Apr 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!*/
!*.*
**/*.out
**/vendor
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package channelsdemo

import (
"bytes"
"fmt"
"runtime/debug"
"sync"
)

func closeChannelsDemo() {

wg := &sync.WaitGroup{}

ch := make(chan int, 1)

wg.Add(2)
go func(ch <-chan int, wg *sync.WaitGroup) {

gr := bytes.Fields(debug.Stack())[1]
fmt.Println("id: ", string(gr))

fmt.Println("message received 1: ", <-ch)
// close(ch)
if msg, ok := <-ch; ok {
fmt.Println(msg, ok)
}
// fmt.Println(<-ch)
// ch <- 5
wg.Done()
}(ch, wg)

go func(ch chan<- int, wg *sync.WaitGroup) {
ch <- 42
fmt.Println("message sent")
close(ch)
//ch <- 50
//fmt.Println(<-ch)
wg.Done()
}(ch, wg)

wg.Wait()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package channelsdemo

import (
"fmt"
"math/rand"
"sync"
)

func forLoop() {
wg := &sync.WaitGroup{}

ch := make(chan int, 1)

wg.Add(2)
go func(ch <-chan int, wg *sync.WaitGroup) {

i := 1
for msg := range ch{
fmt.Println("message received 1: ", msg, i)
i++
}
wg.Done()
}(ch, wg)

go func(ch chan<- int, wg *sync.WaitGroup) {
for i := 0; i < 10; i++ {
ch <- rand.Intn(100)
}
fmt.Println("message sent")
close(ch)
wg.Done()
}(ch, wg)

wg.Wait()
}
22 changes: 22 additions & 0 deletions golang/course/concurrent_programming_with_go/channelsDemo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package channelsdemo

import "fmt"

const (
maxTest = 10
)

func Main() {

// simpleChannelsDemo()

// typedChannelsDemo()

// closeChannelsDemo()

// forLoop()

syncWithRWMutexAndChannel()

fmt.Println("\nend of channel's demo")
}
68 changes: 68 additions & 0 deletions golang/course/concurrent_programming_with_go/channelsDemo/main1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package channelsdemo

import (
"fmt"
"main/shared"
"sync"
"time"
)

func queryDatabase(id int) (shared.Book, bool) {
time.Sleep(100 * time.Millisecond)
for _, b := range shared.Booklist {
if b.ID == id {
return b, true
}
}

return shared.Book{}, false
}

func syncWithRWMutexAndChannel() {
wg := &sync.WaitGroup{}
m := &sync.RWMutex{}
CacheCh := make(chan shared.Book)
dbCh := make(chan shared.Book)

for i := 0; i < maxTest; i++ {
id := shared.Rnd.Intn(8) + 1

wg.Add(2)

go func(id int, wg *sync.WaitGroup, m *sync.RWMutex, ch chan<- shared.Book) {
if b, ok := shared.QueryCache(id); ok {
ch <- b
}
wg.Done()
}(id, wg, m, CacheCh)

go func(id int, wg *sync.WaitGroup, m *sync.RWMutex, ch chan<- shared.Book) {
if b, ok := queryDatabase(id); ok {
m.Lock()
shared.Cache[id] = b
m.Unlock()
ch <- b
} else{
ch <- shared.Book{ID: id}
}
wg.Done()
}(id, wg, m, dbCh)

go func(cacheCh, db <-chan shared.Book) {
select {
case b := <-cacheCh:
fmt.Println("from cache")
fmt.Println(b)
<-dbCh
case b := <-dbCh:
fmt.Println("from database")
fmt.Println(b)
}
}(CacheCh, dbCh)

time.Sleep(120 * time.Millisecond)
//fmt.Printf("Book not found with id : %v\n", id)
}

wg.Wait()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package channelsdemo

import (
"fmt"
"sync"
)

func simpleChannelsDemo() {
wg := &sync.WaitGroup{}

ch := make(chan int, 2)

wg.Add(3)
go func(ch chan int, wg *sync.WaitGroup) {
fmt.Println("message received 1: ", <-ch)
ch <- 0
wg.Done()
}(ch, wg)

go func(ch chan int, wg *sync.WaitGroup) {
ch <- 42
ch <- 50
fmt.Println("message sent")
fmt.Println("message received", <-ch, <-ch)
wg.Done()
}(ch, wg)

go func(ch chan int, wg *sync.WaitGroup) {
fmt.Println("message received 2: ", <-ch)
ch <- 1
wg.Done()
}(ch, wg)

wg.Wait()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package channelsdemo

import (
"fmt"
"sync"
)

func typedChannelsDemo() {

wg := &sync.WaitGroup{}

ch := make(chan int, 1)

wg.Add(2)
go func(ch <-chan int, wg *sync.WaitGroup) {
fmt.Println("message received 1: ", <-ch)
wg.Done()
}(ch, wg)

go func(ch chan<- int, wg *sync.WaitGroup) {
ch <- 42
fmt.Println("message sent")
wg.Done()
}(ch, wg)

wg.Wait()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package concurrencyless

import (
"fmt"
"main/shared"
"time"
)

func Main() {
for i := 0; i < 10; i++ {
id := shared.Rnd.Intn(shared.LenOfBooks()) + 1

if b, ok := shared.QueryCache(id); ok {
fmt.Println("from cache")
fmt.Println(b)
continue
}

if b, ok := shared.QueryDatabase(id); ok {
fmt.Println("from database")
fmt.Println(b)
continue
}

fmt.Printf("Book not found with id : %v", id)
time.Sleep(150 * time.Millisecond)
}

fmt.Println("\nend of general secenario")
}
3 changes: 3 additions & 0 deletions golang/course/concurrent_programming_with_go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module main

go 1.19
37 changes: 37 additions & 0 deletions golang/course/concurrent_programming_with_go/goroutines/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package goroutines

import (
"fmt"
"main/shared"
"time"
)

func Main() {

for i := 0; i < 20; i++ {
id := shared.Rnd.Intn(shared.LenOfBooks()) + 1

fmt.Printf("\nquery no: %d\n", i+1)

go func(id int) {
if b, ok := shared.QueryCache(id); ok {
fmt.Println("from cache")
fmt.Println(b)
}
}(id)

go func(id int) {
if b, ok := shared.QueryDatabase(id); ok {
fmt.Println("from database")
fmt.Println(b)
}
}(id)

fmt.Printf("Book not found with id : %v\n", id)
time.Sleep(150 * time.Millisecond)
}

time.Sleep(1 * time.Second)

fmt.Println("\nend of goroutines")
}
20 changes: 20 additions & 0 deletions golang/course/concurrent_programming_with_go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
channelsdemo "main/channelsDemo"
)

func main() {
fmt.Println("start of concurrent programming with go")

// fmt.Println( reflect.TypeOf(time.Now()).Kind())

// concurrencyless.Main()

// goroutines.Main()

// syncpackage.Main()

channelsdemo.Main()
}
63 changes: 63 additions & 0 deletions golang/course/concurrent_programming_with_go/shared/books.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package shared

import "fmt"

type Book struct {
ID int
Title string
Author string
YearPublished int
}

func (b Book) String() string {
return fmt.Sprintf(""+
"ID: %d\n"+
"\tTitle: %q\n"+
"\tAuthor: %q\n"+
"\tPubliched: %v\n", b.ID, b.Title, b.Author, b.YearPublished)
}

var Booklist = []Book{
{
ID: 1,
Title: "book<ID>",
Author: "book<Author>",
YearPublished: 2000,
},
{
ID: 2,
Title: "book<ID>",
Author: "book<Author>",
YearPublished: 2000,
},
{
ID: 3,
Title: "book<ID>",
Author: "book<Author>",
YearPublished: 2000,
},
{
ID: 4,
Title: "book<ID>",
Author: "book<Author>",
YearPublished: 2000,
},
{
ID: 5,
Title: "book<ID>",
Author: "book<Author>",
YearPublished: 2000,
},
{
ID: 7,
Title: "book<ID>",
Author: "book<Author>",
YearPublished: 2000,
},
{
ID: 8,
Title: "book<ID>",
Author: "book<Author>",
YearPublished: 2000,
},
}
Loading