Skip to content

Instantly share code, notes, and snippets.

View sudaraka94's full-sized avatar
👨‍💻
Building Stuff

Sudaraka Jayathilaka sudaraka94

👨‍💻
Building Stuff
View GitHub Profile
@sudaraka94
sudaraka94 / errors.go
Last active May 3, 2021 05:28
Custom error struct and helpers
package errors
import "github.com/sirupsen/logrus"
type Operation string
type ErrorType string
const (
NotFoundError ErrorType = "NOT_FOUND"
@sudaraka94
sudaraka94 / go_serverless.go
Created June 20, 2020 10:32
Initializing db connections, loggers etc in a go serverless function
var (
db *sql.DB
logger *logging.Logger
once sync.Once
)
// This pattern is used to make the init function easily replaceable in tests
var initFunc = defaultInitFunc
@sudaraka94
sudaraka94 / test_function.go
Created June 20, 2020 10:49
How to test a serverless function in go
func TestF(t *testing.T) {
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
handler := http.HandlerFunc(F)
handler.ServeHTTP(w, r)
resp := w.Result()
if resp.StatusCode != http.StatusOK {
t.Errorf("wrong status code: got %v want %v", resp.StatusCode, http.StatusOK)
@sudaraka94
sudaraka94 / future.go
Created July 11, 2020 11:07
This is an example implementation of futures in Go
package main
import (
"fmt"
"time"
)
var users = map[int]string{
1: "Rob",
2: "Ken",
@sudaraka94
sudaraka94 / producer_consumer.go
Created July 11, 2020 15:05
Example of producer consumer concurrency pattern in Go
package main
import (
"fmt"
"strings"
)
var users = map[int]string{
1: "Rob",
2: "Ken",
@sudaraka94
sudaraka94 / main.go
Created July 16, 2021 09:42
Embedding Files in Golang
package main
import (
"embed"
"log"
"net/http"
)
//go:embed static/*
var staticContent embed.FS
@sudaraka94
sudaraka94 / main.go
Created August 13, 2021 09:21
Data streaming demo to a csv file in go
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
)
type userInfo struct {