Skip to content

Commit

Permalink
[feat] push other examples, but ci only tcp-client-server
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsucho committed Dec 21, 2023
1 parent c9b393f commit 0a0c9ab
Show file tree
Hide file tree
Showing 24 changed files with 494 additions and 1 deletion.
3 changes: 3 additions & 0 deletions atomic/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/tomsucho/go-examples/atomic

go 1.20
25 changes: 25 additions & 0 deletions atomic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

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

func main() {

var counter int32 = 0
var wg sync.WaitGroup

for i := 0; i < 5000; i++ {
for j := 0; j < 10; j++ {
wg.Add(1)
go func() {
defer wg.Done()
atomic.AddInt32(&counter, 1)
}()
}
}
wg.Wait()
fmt.Println("Counter:=", counter)
}
29 changes: 29 additions & 0 deletions channel-sync/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import "fmt"

func main() {

jobs := make(chan int)
done := make(chan bool)

go func() {
for {
if j, more := <-jobs; more {
fmt.Println("Job handler: received", j)
} else {
fmt.Println("Job handler: channel closed..")
done <- true
return
}
}
}()

for i := 0; i < 10; i++ {
fmt.Println("Sending job", i)
jobs <- i
}
close(jobs)
<-done

}
41 changes: 41 additions & 0 deletions channels/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"sync"
)

var wg sync.WaitGroup

func main() {
m := make(chan string, 11)

for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
m <- fmt.Sprintf("Id: %d", i)
fmt.Println("Sent:", i)
}(i)
if i == 9 {
//time.Sleep(1 * time.Second)
close(m)
}

}
// wg.Add(1)
// go func(t time.Duration) {
// defer wg.Done()
// time.Sleep(t * time.Minute)
// }(1)

//wg.Wait()
for {
if val, ok := <-m; ok {
fmt.Println("Received:", val)
} else {
break
}
}
fmt.Println("Finished!")
}
34 changes: 34 additions & 0 deletions errors/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
)

type myError struct {
id int
msg string
}

func (my_err myError) Error() string {
return fmt.Sprintf("ERROR: id=%d, msg=%s", my_err.id, my_err.msg)
}

func divider(a, b int) (int, error) {
if b == 0 {
return 0, myError{id: 1, msg: "Can't divide by 0!"}
} else {
return a / b, nil
}

}

func main() {

fmt.Println("Hello World!")
if v, err := divider(4, 0); err == nil {
fmt.Printf("Result = %d", v)
} else {
fmt.Println(err)
}

}
2 changes: 2 additions & 0 deletions file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a smaple text!
La la la!
7 changes: 7 additions & 0 deletions files/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

func check(e error) {
if e != nil {
panic(e)
}
}
24 changes: 24 additions & 0 deletions files/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
"os"
)

func main() {
p := fmt.Println
pf := fmt.Printf

str := `This is a smaple text!
La la la!`
err := os.WriteFile("file.txt", []byte(str), 0644)

check(err)

if data, err := os.ReadFile("file.txt"); err == nil {
pf("%s\n", string(data))
} else {
p(err)
}

}
2 changes: 1 addition & 1 deletion go.work
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ go 1.20

use (
.
./tcp-server
./tcp-client-server
)
48 changes: 48 additions & 0 deletions interfaces/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"math"
)

type geometry interface {
area() float64
perimeter() float64
}

type rectangle struct {
width float64
height float64
}

type circle struct {
radius float64
}

func (c circle) area() float64 {
return 3.14 * math.Pow(c.radius, 2)
}

func (r rectangle) area() float64 {
return r.height * r.width
}

func (r rectangle) perimeter() float64 {
return r.height*2 + r.width*2
}

func (c circle) perimeter() float64 {
return 2 * 3.14 * c.radius
}

func main() {
rect := rectangle{height: 5, width: 10}
fmt.Println(rect.area())

geo := geometry(rect)
fmt.Println(geo.area())

circ := circle{radius: 10}
geo = geometry(circ)

}
37 changes: 37 additions & 0 deletions json/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"encoding/json"
"fmt"
"os"
)

type data struct {
S string `json:"blah"`
V int `json:"num"`
}

func main() {

if strB, err := json.Marshal([]int{1, 2, 3}); err == nil {
fmt.Println(strB)
} else {
fmt.Println(err)
}

if str, err := json.Marshal(data{S: "test", V: 1}); err == nil {
enc := json.NewEncoder(os.Stdout)
enc.Encode(data{S: "test", V: 1})
fmt.Println(string(str))
} else {
fmt.Println(err)
}

d := []byte(`{"name": "John", "numbers": [{"age": 42}, {"height": 182}]}`)
var d_str map[string]interface{}
if err := json.Unmarshal(d, &d_str); err == nil {
fmt.Println(d_str)
} else {
fmt.Println(err)
}
}
43 changes: 43 additions & 0 deletions mutex/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

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

type Container struct {
mtx sync.Mutex
items map[string]int
}

func (c *Container) inc(key string, i int, delay time.Duration) {
c.mtx.Lock()
time.Sleep(delay)
c.items[key] += 1
defer c.mtx.Unlock()
fmt.Printf("%d: incremented: %d\n", i, c.items[key])
}

var wg sync.WaitGroup

func main() {
c := Container{
items: map[string]int{"a": 0, "b": 0},
}

rand.Seed(time.Now().UnixNano())

for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int, t time.Duration) {
defer wg.Done()
c.inc("a", i, t)
//c.inc("b", i)
}(i, time.Duration(rand.Intn(3)))

}
wg.Wait()
fmt.Println(c.items)
}
19 changes: 19 additions & 0 deletions parsing_url/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"net/url"
)

func main() {
p := fmt.Println
s := "postgres://user:[email protected]:5432/path?k=v#f"

u, _ := url.Parse(s)
p(u.Scheme)
p(u.User)
p(u.Query())
p(u.Path)
p(u.Fragment)
p(u.RawQuery)
}
30 changes: 30 additions & 0 deletions select/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"fmt"
"time"
)

func simulate_work(s string, c chan string, t time.Duration) {
time.Sleep(t)
c <- fmt.Sprintf("%s finished!", s)
}

func main() {
c1 := make(chan string)
c2 := make(chan string)

go simulate_work("c1", c1, 3*time.Second)
go simulate_work("c2", c2, time.Second)

//for i := 0; i < 2; i++ {
select {
case s := <-c1:
fmt.Println(s)
case s := <-c2:
fmt.Println(s)
case <-time.After(1 * time.Second):
fmt.Println("Timeout!")
}
//}
}
Loading

0 comments on commit 0a0c9ab

Please sign in to comment.