Skip to content

Instantly share code, notes, and snippets.

Avatar

Florian Beeres cideM

View GitHub Profile
@cideM
cideM / main.go
Created Oct 18, 2022
Final main.go
View main.go
package main
import (
"context"
"errors"
"log"
"strings"
"sync"
"time"
@cideM
cideM / main.go
Created Sep 8, 2022
New main.go file before the "Error handling" chapter
View main.go
package main
import (
"context"
"errors"
"log"
"runtime"
"strings"
"sync"
"time"
View anki.nix
{
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
The new main function with generics
View twelve.go
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 Sep 8, 2022
The new step function
View eleven.go
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 Sep 8, 2022
Using semaphores
View ten.go
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 Sep 20, 2021
generics to fight boilerplate
View nine.diff
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 Sep 8, 2022
Add error handling
View eight.diff
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 Sep 8, 2022
Fix the empty logs
View seven.diff
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 Sep 8, 2022
Flood the terminal with empty logs
View six.diff
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 {