-
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.
[feat] push other examples, but ci only tcp-client-server
- Loading branch information
tomsucho
committed
Dec 21, 2023
1 parent
c9b393f
commit 0a0c9ab
Showing
24 changed files
with
494 additions
and
1 deletion.
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,3 @@ | ||
module github.com/tomsucho/go-examples/atomic | ||
|
||
go 1.20 |
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" | ||
"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) | ||
} |
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 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 | ||
|
||
} |
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,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!") | ||
} |
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,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) | ||
} | ||
|
||
} |
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,2 @@ | ||
This is a smaple text! | ||
La la la! |
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,7 @@ | ||
package main | ||
|
||
func check(e error) { | ||
if e != nil { | ||
panic(e) | ||
} | ||
} |
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 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) | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -2,5 +2,5 @@ go 1.20 | |
|
||
use ( | ||
. | ||
./tcp-server | ||
./tcp-client-server | ||
) |
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,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) | ||
|
||
} |
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,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) | ||
} | ||
} |
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 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) | ||
} |
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,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) | ||
} |
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,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!") | ||
} | ||
//} | ||
} |
Oops, something went wrong.