Skip to content

Instantly share code, notes, and snippets.

View sgrankin's full-sized avatar
🎏

Sergey Grankin sgrankin

🎏
  • 15:59 (UTC -04:00)
View GitHub Profile
/*
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.
"editor.semanticTokenColorCustomizations": {
"[Massimo]": {
"enabled": true,
"rules": {
"*": { "foreground": "#090909" },
// Semantic token classification:
// Modules
"namespace": { "foreground": "#003D67" },
// Nominal Types
"type": { "foreground": "#C000C0" },
package main
import (
"fmt"
)
type Result(type T) struct {
Val T
Err error
}

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

// 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)
};
// 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 {
// 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)]
@sgrankin
sgrankin / tagged.scala
Created July 14, 2017 15:24
scala tagged types
import com.twitter.bijection.Bijection
package object tagging {
sealed trait Tagged[+V, +T]
type @@[+V, +T] = V with Tagged[V, T]
object Tagged {
implicit class Untaggable[V, T](val tagged: V @@ T) extends AnyVal {
@inline def untag: V = tagged
}
package main
import java.util
import java.util.concurrent.RejectedExecutionException
import com.twitter.app.App
import com.twitter.concurrent.Permit
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.util.HashedWheelTimer
import com.twitter.finagle.{Http, Service, param => fparam, _}
@sgrankin
sgrankin / ending.gcode
Created December 23, 2016 03:35
m2 simplify3d scripts
M140 S0 ; turn off bed
G91 ; relative mode
G1 Z20 ; lift
G90 ; absolute mode
G92 E18 ; extruder relative pos
G1 F300 ; set speed
G1 E0 ; retract filament
G92 E0 ; extruder relative pos