Skip to content

Commit

Permalink
Added golang examples: mutex, wg, timer, ticker. And some about inter…
Browse files Browse the repository at this point in the history
…faces
  • Loading branch information
k8s4 committed Oct 13, 2024
1 parent 407a677 commit 3ead36b
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 11 deletions.
46 changes: 36 additions & 10 deletions examples/golang/04b_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,24 @@ func (p *Phone) Send(msg string) error {
return nil
}

func Notify(s Sender) {
err := s.Send("Notify message")
if err != nil {
fmt.Println(err)
func Notify(i interface{}) {
switch i.(type) {
case int:
fmt.Println("Int not supported...")
}

s, ok := i.(Sender)
if !ok {
fmt.Println("Can't make interface")
return
}
switch s.(type) {
case *Email:
fmt.Println("Sucksess to email")
case *Phone:
phone := s.(*Phone)
fmt.Println(phone.Balance)

err := s.Send("Empty message fom interface")
if err != nil {
fmt.Println("Error")
return
}
fmt.Println("Success")
}

func main() {
Expand All @@ -65,6 +70,27 @@ func main() {

phone := &Phone{100500, 300}
Notify(phone)

Notify(2)
Notify("Strinnnnggg")

some := [3]int64{1,2,3}
Notify(some)
}


func OldNotify(s Sender) {
err := s.Send("Notify message")
if err != nil {
fmt.Println(err)
return
}
switch s.(type) {
case *Email:
fmt.Println("Sucksess to email")
case *Phone:
phone := s.(*Phone)
fmt.Println(phone.Balance)
}
}

2 changes: 1 addition & 1 deletion examples/golang/05b_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package main

import ("fmt"
// "runtime"
"errors"
// "errors"
"time")


Expand Down
33 changes: 33 additions & 0 deletions examples/golang/05d_mutex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"fmt"
"sync"
"sync/atomic"
"time"
)


func addSome(add1 *int, add2 *int32, mu *sync.Mutex) {
mu.Lock()
*add1 = *add1 + 1
mu.Unlock()

atomic.AddInt32(add2, 1)
}

func main() {
var some = 0
var some_atomic int32 = 10

mu := &sync.Mutex{}

for i := 1; i <= 1000; i++ {
go addSome(&some, &some_atomic, mu)
}

time.Sleep(1 * time.Second)

fmt.Println(some, some_atomic)
}

30 changes: 30 additions & 0 deletions examples/golang/05e_waitgroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"fmt"
"sync"
"time"
)

func sleep(t time.Duration, wg *sync.WaitGroup) {
fmt.Println("Sleep: ", t)
time.Sleep(t)
fmt.Println("Sleep: ", t)
wg.Done()
}

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

wg.Add(1)
go sleep(1 * time.Second, wg)

wg.Add(1)
go sleep(2 * time.Second, wg)

wg.Add(1)
go sleep(3 * time.Second, wg)

wg.Wait()
}

32 changes: 32 additions & 0 deletions examples/golang/05f_timer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"time"
)

func someWithTimer(t *time.Timer, q chan int) {
time.Sleep(1 * time.Second)
select {
case <- t.C:
fmt.Println("Time out")
case <- q:
if !t.Stop() {
<- t.C
}
fmt.Println("Timer was stopped")
default:
fmt.Println("End function")
}
}



func main() {
timer := time.NewTimer(1 * time.Second)
quit := make(chan int)

go someWithTimer(timer, quit)

time.Sleep(2 * time.Second)
}
22 changes: 22 additions & 0 deletions examples/golang/05g_ticker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
"time"
)


func main() {
ticker := time.NewTicker(1 * time.Second)

count := 0
for tick := range ticker.C {
count++
fmt.Printf("Super tick #%v, time %v\n", count, tick)
if count > 4 {
ticker.Stop()
break
}
}

}

0 comments on commit 3ead36b

Please sign in to comment.