Skip to content

Instantly share code, notes, and snippets.

View odeke-em's full-sized avatar

Emmanuel T Odeke odeke-em

View GitHub Profile
@odeke-em
odeke-em / main.go
Created August 21, 2016 03:16
File, Function name and line information for debugging call sites
package main
import (
"fmt"
"runtime"
)
func debugPrintf(fmt_ string, args ...interface{}) {
programCounter, file, line, _ := runtime.Caller(1)
fn := runtime.FuncForPC(programCounter)
@odeke-em
odeke-em / marshalJSON_unmarshalJSON.go
Last active December 30, 2022 10:44
Example on custom UnmarshalJSON and MarshalJSON
package main
import (
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
)
@odeke-em
odeke-em / log.md
Created October 29, 2016 18:25
Inspecting bodies and requests from various methods
$ for method in "PATCH" "DELETE" "POST" "GET" "PUT" "AMETHOD";do go run main.go --body hello --method $method >> log;done && cat log
PATCH / HTTP/1.1
Host: 127.0.0.1:64022
Accept-Encoding: gzip
Content-Length: 5
User-Agent: Go-http-client/1.1

hello
DELETE / HTTP/1.1
@odeke-em
odeke-em / cancellation-on-success.go
Created December 28, 2016 20:16
Demo how to cancel asynchronous processing after getting exactly n successful results
package main
import (
"fmt"
"time"
"github.com/odeke-em/semalim"
)
type echo struct {
@odeke-em
odeke-em / full-snippet.go
Created February 20, 2017 08:34
full snippet for the earthquakes compile type assertions
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"strconv"
)
@odeke-em
odeke-em / recursiveJSONParse.js
Created March 5, 2017 02:36
Code to recursively parse through JSON that was extra quoted/stringified in multiple nests.
// Function to recursively parse through JSON
// that was extra quoted during stringifying for
// nested levels. This function undoes all that
// work and makes JSON objects for every object
// that it can traverse.
function recursiveJSONParse(obj) {
if (!obj)
return obj;
switch (typeof obj) {
package main
import (
"fmt"
"log"
"github.com/orijtech/500px/v1"
)
func main() {
@odeke-em
odeke-em / numericBool.go
Created May 19, 2017 20:12
Numeric bool: serialized as int <--> used in code like a boolean
package main
import (
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
)
@odeke-em
odeke-em / darksi.de.go
Created June 5, 2017 07:43
HTTPS-ify darksi.de with frontender: generate a binary that could be uploaded directly to cloud storage then pulled and deployed on server
package main
import (
"io"
"log"
"os"
"github.com/orijtech/frontender"
)
@odeke-em
odeke-em / uber-ride.go
Created June 6, 2017 07:14
Requesting an Uber in Go
package main
import (
"fmt"
"log"
"github.com/orijtech/uber/v1"
)
func main() {