Skip to content

Instantly share code, notes, and snippets.

View cideM's full-sized avatar

Florian Beeres cideM

View GitHub Profile
@cideM
cideM / main.go
Created October 18, 2022 08:45
Final main.go
package main
import (
"context"
"errors"
"log"
"strings"
"sync"
"time"
@cideM
cideM / main.go
Created September 8, 2022 13:48
New main.go file before the "Error handling" chapter
package main
import (
"context"
"errors"
"log"
"runtime"
"strings"
"sync"
"time"
@cideM
cideM / anki.nix
Created September 27, 2021 11:54
anki
{
description = "zola";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.nixpkgs.url = "github:nixos/nixpkgs/staging-next";
inputs.ankisrc.url = "https://github.com/ankitects/anki/releases/download/2.1.35/anki-2.1.35-linux-amd64.tar.bz2";
inputs.ankisrc.flake = false;
outputs = { self, nixpkgs, flake-utils, ankisrc }:
flake-utils.lib.eachDefaultSystem (system:
@cideM
cideM / twelve.go
Last active May 19, 2022 09:53
The new main function with generics
func main() {
source := []string{"FOO", "BAR", "BAX"}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
readStream, err := producer(ctx, source)
if err != nil {
log.Fatal(err)
}
@cideM
cideM / eleven.go
Last active September 8, 2022 13:41
The new step function
func step[In any, Out any](
ctx context.Context,
inputChannel <-chan In,
fn func(In) (Out, error),
) (chan Out, chan error) {
outputChannel := make(chan Out)
errorChannel := make(chan error)
limit := int64(2)
// Use all CPU cores to maximize efficiency. We'll set the limit to 2 so you
@cideM
cideM / ten.go
Last active September 8, 2022 13:41
Using semaphores
func main() {
source := []string{"FOO", "BAR", "BAX"}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
inputChannel, err := producer(ctx, source)
if err != nil {
log.Fatal(err)
}
@cideM
cideM / nine.diff
Created September 20, 2021 08:29
generics to fight boilerplate
diff --git a/main.go b/main.go
index b850a16..6745855 100644
--- a/main.go
+++ b/main.go
@@ -121,7 +121,7 @@ func main() {
errors = append(errors, lowerCaseErrors)
}
- stage1Merged := mergeStringChans(ctx, stage1Channels...)
+ stage1Merged := mergeChans(ctx, stage1Channels...)
@cideM
cideM / eight.diff
Last active September 8, 2022 13:40
Add error handling
diff --git a/main.go b/main.go
index 3888a36..8794caa 100644
--- a/main.go
+++ b/main.go
@@ -2,6 +2,7 @@ package main
import (
"context"
+ "errors"
"log"
@cideM
cideM / seven.diff
Last active September 8, 2022 12:26
Fix the empty logs
diff --git a/main.go b/main.go
index c45418b..dd7d1ef 100644
--- a/main.go
+++ b/main.go
@@ -31,9 +31,10 @@ func sink(ctx context.Context, values <-chan string) {
log.Print(ctx.Err().Error())
return
case val, ok := <-values:
- log.Println(val)
if ok {
@cideM
cideM / six.diff
Last active September 8, 2022 12:26
Flood the terminal with empty logs
diff --git a/main.go b/main.go
index 08d7580..c45418b 100644
--- a/main.go
+++ b/main.go
@@ -31,6 +31,7 @@ func sink(ctx context.Context, values <-chan string) {
log.Print(ctx.Err().Error())
return
case val, ok := <-values:
+ log.Println(val)
if ok {