Skip to content

Instantly share code, notes, and snippets.

View sgrankin's full-sized avatar
🎏

Sergey Grankin sgrankin

🎏
  • 07:56 (UTC -04:00)
View GitHub Profile
// WARNING: mysql versions before 5.7.3 do not properly use index lookups for this construction.
trait TupleQuery { self: io.getquill.context.Context[_, _] =>
/**
* Allows `where (x, y) in ((c1, c2))` queries via `liftTuples(tuples).contains((p.name, p.age))`
* See https://github.com/getquill/quill/issues/651
*/
def liftTuples[T1: Encoder, T2: Encoder](
tuples: Seq[(T1, T2)]
// Alternate construction of tupled where in queries for quill for mysql 5.6
// WARNING: triggers O(n^2) behavior so is CPU intensive for N > 100
trait TupleQuery { self: io.getquill.context.Context[_, _] =>
/**
* Allows (x = c1 AND y = c2) OR (x = c3 AND y = c4) queries via `inTuples(tuples).apply(r.x, r.y)`
*/
def inTuples[T1: Encoder, T2: Encoder](tuples: Seq[(T1, T2)]): Quoted[(T1, T2) => Boolean] = {
tuples match {
// inspired by clojure's ->, pipeline.rs, and s-expressions
macro_rules! pipe_fun {
// [as t] => it as t
([as $typ:ty], $ret:expr) => {
$ret as $typ;
};
// ? => try!(it)
(?, $ret:expr) => {
try!($ret)
};

Sergey’s Go “style” guide

This is a personal interpretation of Go’s nonexistent style/conventions guide with some experience-based kvetching & pet peeves thrown in; it’s a highlights reel and supplement to other guides.

Zen

  • There are trade offs to everything. This is a sign, not a cop.
  • Most of this advice/preferences comes down to keeping things ‘simple’—reducing interdependencies and knowledge duplication within the code (including textual clutter) and keeping things readable for Sergey (as I often, and repeatedly, skim code).
  • Go is an opinionated language written by opinionated C programmers. There are better ways to write code in other languages that are hard to do in Go, by design. Don’t fight it—sometimes you just have to write a for loop and that’s OK.

Other references you want to read

package main
import (
"fmt"
)
type Result(type T) struct {
Val T
Err error
}
"editor.semanticTokenColorCustomizations": {
"[Massimo]": {
"enabled": true,
"rules": {
"*": { "foreground": "#090909" },
// Semantic token classification:
// Modules
"namespace": { "foreground": "#003D67" },
// Nominal Types
"type": { "foreground": "#C000C0" },
/*
Demo of so_reuseport.
go run main.go # Start the first server
go run main.go -client # Start the client.
# Wait for client to start echoing responses.
# Each request sleeps for 5 seconds.
go run main.go # Start a second server.