Skip to content

Instantly share code, notes, and snippets.

View tebeka's full-sized avatar
💭
alive

Miki Tebeka tebeka

💭
alive
View GitHub Profile
@tebeka
tebeka / sema.go
Created May 8, 2024 14:35
Semaphore using buffered channel.
package main
import (
"fmt"
"log"
"math/rand/v2"
"sync"
"sync/atomic"
"time"
)
@tebeka
tebeka / httpd.go
Created February 15, 2024 13:40
Monitoring number of open files on Linux in Go
package main
import (
"expvar"
"fmt"
"log/slog"
"net/http"
"os"
"time"
@tebeka
tebeka / linspace.go
Created February 11, 2024 16:08
Linear space with Go's "range over function" experiment
// export GOEXPERIMENT=rangefunc
package main
import "fmt"
func main() {
for v := range LinearSpace(0, 3, 10) {
fmt.Printf("%.3f ", v)
}
fmt.Println()
@tebeka
tebeka / 2.proto
Last active November 23, 2023 06:46
syntax = "proto3";
import "google/protobuf/timestamp.proto";
message User {
int64 id = 1;
google.protobuf.Timestamp created = 2;
}
@tebeka
tebeka / 1.py
Created November 23, 2023 06:38
@app.get("/users/{user_id}")
def get_user(user_id):
user = app.state.db.get_user(user_id)
if user is None:
raise HTTPException(HTTPStatus.NOT_FOUND)
data = json.dumps(asdict(user))
return Response(data, media_type='application/json')
/*
#golang #gem: Use expvar to expose metrics on /debug/vars.
*/
package main
import (
"expvar"
"fmt"
"log"
"net/http"
@tebeka
tebeka / 6.txt
Created November 29, 2022 12:30
Note that the Go == operator compares not just the time instant but also the Location and the monotonic clock reading. Therefore, Time values should not be used as map or database keys without first guaranteeing that the identical Location has been set for all values, which can be achieved through use of the UTC or Local method, and that the monotonic clock reading has been stripped by setting t = t.Round(0). In general, prefer t.Equal(u) to t == u, since t.Equal uses the most accurate comparison available and correctly handles the case when only one of its arguments has a monotonic clock reading.
@tebeka
tebeka / 5.go
Created November 29, 2022 12:30
fmt.Println(t1.Equal(t2))
@tebeka
tebeka / 4.txt
Created November 29, 2022 12:30
t1: 2022-11-29 14:21:10.981666007 +0200 IST m=+0.000013388
t2: 2022-11-29 14:21:10.981666007 +0200 IST
@tebeka
tebeka / 3.go
Created November 29, 2022 12:30
fmt.Println("t1:", t1)
fmt.Println("t2:", t2)