diff --git a/atomic/go.mod b/atomic/go.mod deleted file mode 100644 index d283dce..0000000 --- a/atomic/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/tomsucho/go-examples/atomic - -go 1.20 diff --git a/atomic/main.go b/atomic/main.go deleted file mode 100644 index fc6513b..0000000 --- a/atomic/main.go +++ /dev/null @@ -1,25 +0,0 @@ -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) -} diff --git a/channel-sync/main.go b/channel-sync/main.go deleted file mode 100644 index 390b700..0000000 --- a/channel-sync/main.go +++ /dev/null @@ -1,29 +0,0 @@ -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 - -} diff --git a/channels/main.go b/channels/main.go deleted file mode 100644 index d650d28..0000000 --- a/channels/main.go +++ /dev/null @@ -1,41 +0,0 @@ -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!") -} diff --git a/errors/main.go b/errors/main.go deleted file mode 100644 index 56d3a8a..0000000 --- a/errors/main.go +++ /dev/null @@ -1,34 +0,0 @@ -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) - } - -} diff --git a/files/check.go b/files/check.go deleted file mode 100644 index d9f9f75..0000000 --- a/files/check.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -func check(e error) { - if e != nil { - panic(e) - } -} diff --git a/files/read.go b/files/read.go deleted file mode 100644 index 5b12a02..0000000 --- a/files/read.go +++ /dev/null @@ -1,24 +0,0 @@ -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) - } - -} diff --git a/interfaces/main.go b/interfaces/main.go deleted file mode 100644 index f8760f1..0000000 --- a/interfaces/main.go +++ /dev/null @@ -1,48 +0,0 @@ -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) - -} diff --git a/json/main.go b/json/main.go deleted file mode 100644 index 9cb3b66..0000000 --- a/json/main.go +++ /dev/null @@ -1,37 +0,0 @@ -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) - } -} diff --git a/mutex/main.go b/mutex/main.go deleted file mode 100644 index aac08da..0000000 --- a/mutex/main.go +++ /dev/null @@ -1,43 +0,0 @@ -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) -} diff --git a/parsing_url/main.go b/parsing_url/main.go deleted file mode 100644 index c8f3594..0000000 --- a/parsing_url/main.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import ( - "fmt" - "net/url" -) - -func main() { - p := fmt.Println - s := "postgres://user:pass@host.com: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) -} diff --git a/select/main.go b/select/main.go deleted file mode 100644 index 1072014..0000000 --- a/select/main.go +++ /dev/null @@ -1,30 +0,0 @@ -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!") - } - //} -} diff --git a/sorting/main.go b/sorting/main.go deleted file mode 100644 index 5ee7193..0000000 --- a/sorting/main.go +++ /dev/null @@ -1,47 +0,0 @@ -package main - -import ( - "fmt" - "sort" -) - -type byLen []string - -func (b byLen) Len() int { - return len(b) -} - -func (b byLen) Swap(i, j int) { - b[i], b[j] = b[j], b[i] -} - -func (b byLen) Less(i, j int) bool { - return len(b[i]) < len(b[j]) -} - -func main() { - - test := []string{"apple", "joe", "bananas"} - sort.Sort(byLen(test)) - fmt.Println(test) - - s := "abcdefg🔥" - txt := []rune(s) - reversed := "" - - // for idx, c := range s { - // txt[idx] = c - // } - - for i := len(txt) - 1; i >= 0; i-- { - // fmt.Printf("Rune %d: %s\n", i, string(txt[i])) - reversed = reversed + string(txt[i]) - } - fmt.Println(s) - fmt.Println(reversed) - - // Sorting - nums := []int{9, 5, 7, 1, 4} - sort.Ints(nums) - fmt.Println(nums) -} diff --git a/testing/intutils.go b/testing/intutils.go deleted file mode 100644 index 8879d30..0000000 --- a/testing/intutils.go +++ /dev/null @@ -1,8 +0,0 @@ -package intutils - -func IntMin(a, b int) int { - if a < b { - return a - } - return b -} diff --git a/testing/intutils_test.go b/testing/intutils_test.go deleted file mode 100644 index 736b356..0000000 --- a/testing/intutils_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package intutils - -import "testing" - -func TestIntMin(t *testing.T) { - a, b, wants := 2, -2, -2 - res := IntMin(a, b) - if res != wants { - t.Errorf("IntMin(%d,%d) wants %d", a, b, wants) - } -} diff --git a/ticker/main.go b/ticker/main.go deleted file mode 100644 index e3ce1aa..0000000 --- a/ticker/main.go +++ /dev/null @@ -1,26 +0,0 @@ -package main - -import ( - "fmt" - "time" -) - -func main() { - - ticker := time.NewTicker(time.Second) - done := make(chan bool) - - go func() { - for { - select { - case <-done: - fmt.Println("Closing!") - return - case t := <-ticker.C: - fmt.Println("Ticket at:", t) - } - } - }() - time.Sleep(10 * time.Second) - done <- true -} diff --git a/time/main.go b/time/main.go deleted file mode 100644 index a749aa7..0000000 --- a/time/main.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "fmt" - "time" -) - -func main() { - - p := fmt.Println - - t := time.Now() - p(t.Add(time.Hour * 24)) - then := t.AddDate(0, 1, 0) - p(then) - p(then.Sub(t)) - - future := time.Date(2023, 7, 28, 14, 30, 20, 1000, time.Local) - p(future) -} diff --git a/workers/main.go b/workers/main.go deleted file mode 100644 index 83f89c2..0000000 --- a/workers/main.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "fmt" - "time" -) - -func worker(id int, jobs chan int, results chan string) { - for job := range jobs { - fmt.Printf("Worker id:=%d starting job:=%d\n", id, job) - time.Sleep(time.Second) - results <- fmt.Sprintf("Worker id:=%d finished job:=%d\n", id, job) - } -} - -func main() { - - numJobs := 10 - jobs := make(chan int, numJobs) - results := make(chan string, numJobs) - - for w := 0; w < 2; w++ { - go worker(w, jobs, results) - } - - for j := 0; j < numJobs; j++ { - jobs <- j - } - close(jobs) - - for k := 0; k < numJobs; k++ { - fmt.Println(<-results) - } -}