Skip to content

Instantly share code, notes, and snippets.

Go Interface Design Guidelines

Guiding Principle

Define interfaces at the point of use, not at the point of implementation. — Idiomatic Go best practice


Why This Matters

package utils
import (
"context"
"time"
)
// RetryConfig defines settings for retry behavior
type RetryConfig struct {
Attempts int // Total number of attempts (including initial)
export type RetryConfig = {
attempts?: number; // Total number of attempts (including initial)
baseDelayMs?: number; // Starting delay in milliseconds
maxDelayMs?: number; // Maximum delay in milliseconds
retryIf?: (err: any) => boolean; // Optional: Retry only if this returns true
onRetry?: (attempt: number, err: any) => void; // Optional: Called after each failed attempt
};
/**
* Delays execution for a given number of milliseconds.
@hamstag
hamstag / laravel-webhook.php
Created May 15, 2025 06:35
Laravel Webhook
// routes/api.php
Route::post('/webhook', function (Request $request) {
$signature = $request->header('X-Signature');
$calculatedHash = hash_hmac('sha256', $request->getContent(), env('WEBHOOK_SECRET'));
return [
'message' => '',
];
});
import Redis, { Callback, Result } from "ioredis";
const SCRIPT_TOKEN_BUCKET = `
-- Returns 1 if allowed, 0 if not
local key = KEYS[1]
local max = tonumber(ARGV[1])
local refillIntervalSeconds = tonumber(ARGV[2])
local cost = tonumber(ARGV[3])
local now = tonumber(ARGV[4]) -- Current unix time in seconds
@hamstag
hamstag / Makefile
Last active September 17, 2024 05:21
deploy script
VERSION ?= latest
REGISTRY ?= registry-name
# export SERVER_ADDRESS="111.222.333.444"
SERVER_ADDRESS ?= ${SERVER_ADDRESS}
# Commands
deploy: build save upload run-deploy remove-file
@hamstag
hamstag / pubnub.js
Created September 5, 2024 07:25
PubNub mock test
'use strict';
class PubNub {
constructor(config) {
this.listener = {}
}
addListener(params) {
this.listener = params;
}
subscribe(params) {
@hamstag
hamstag / flutter-dependency-injection-example.dart
Last active August 7, 2024 02:58
Flutter dependency injection example
abstract class LocalStorage {
Future<void> add(String key, Object? value);
Future<T> read<T>(String key, T defaultValue);
Future<void> delete(String key);
}
class SecureStorage implements LocalStorage {
late FlutterSecureStorage storage;
SecureStorage() {
@hamstag
hamstag / debounce-input-react.js
Created June 26, 2024 03:25
Debounce Input in React
import React from "react";
const DebounceInput = () => {
const [inputValue, setInputValue] = React.useState("");
const [debouncedInputValue, setDebouncedInputValue] = React.useState("");
const handleInputChange = (event) => {
setInputValue(event.target.value);
};
@hamstag
hamstag / react-form-multi-input.js
Created June 25, 2024 04:26
How to handle multiple inputs in React
import React, { useState } from "react";
const initialValues = {
company: "",
position: "",
link: "",
date: "",
note: "",
};