Skip to content

Instantly share code, notes, and snippets.

@vidhill
vidhill / teereader.go
Created September 16, 2022 13:55
Make copy of response body reader, and dump json
package main
import (
"io"
"net/http"
"os"
"strings"
)
func dummmy(){
@vidhill
vidhill / main.go
Created February 24, 2022 13:28
Go, print numbers with thousand separators
package main
import (
"golang.org/x/text/language"
"golang.org/x/text/message"
)
// create a new printer withe the locale set to english
var print = message.NewPrinter(language.English)
@vidhill
vidhill / go.mod
Last active February 3, 2022 14:46
Go: get the value of the json tag from a `struct` instance
module dummy/foo
go 1.14
require (
)
@vidhill
vidhill / dump_raw_response.go
Last active February 24, 2022 13:29
Go, how to dump `http.Response` body to log output using an `io.Writer`, -for debugging purposes
import (
"io"
"log"
"io"
"net/http"
)
func dumpResponse(r http.Response) {
io.Copy(os.Stdout, r.Body)
log.Println("")
const getPromise = function (resolveValue) {
return Promise.resolve({ result: resolveValue });
};
const handleUsingThen = function () {
return getPromise("doA").then((res) => res.result);
};
const handleAsync = async function () {
const res = await getPromise("doB");
const getDelayed = function (resolveVal) {
return new Promise((resolve) =>
setTimeout(() => resolve("delayed: " + resolveVal), 2000)
);
};
const func1 = function () {
console.log("Hello func1");
return getDelayed("func1");
};
const doFoo = (passedObj) => {
passedObj.foo = true;
passedObj.bar = false;
return {
cat: 'meow',
passedObj
};
}
@vidhill
vidhill / service.js
Last active September 6, 2019 19:09
Please avoid this pattern
import { someApiCall } from './'
const myService = async function(parameter, passedObject) {
const restResponse = await someRestCall(parameter);
passedObject.details = restResponse[0];
return {
content: restResponse.foo,
@vidhill
vidhill / reduce.java
Last active November 9, 2018 13:46
Java reduce/collection, the many ways to skin a cat
// Option A
Integer totalFixedRateResultCount = fixedRateCounts
.stream()
.reduce(0, (aggValue, current) -> {
Integer value = (Integer) current.getValue();
return aggValue + value;
}, (aLong1, aLong2) -> aLong1);
// Option B
Integer totalFixedRateResultCount = fixedRateCounts
@vidhill
vidhill / kube-get-pods-service.sh
Created June 1, 2018 20:55
Kubectl: workaround to get a list of pods for a service
#!/bin/bash
if [ "$1" != "" ]; then
echo "Get pods in service: $1"
else
echo "Service blank"
echo "Pass the target service"
exit 1
fi