Skip to content

Instantly share code, notes, and snippets.

@cn007b
cn007b / DRYinGO.3.go
Last active January 30, 2019 23:22
DRY in GO #3
func withEmployee(id string, cb func(Employee) (string, int)) (string, int) {
e, err := db.GetEmployeeByID(id)
if err != nil {
// error handling provided earlier
}
return cb(e)
}
func GetEmployeeName(id string) (string, int) {
@cn007b
cn007b / DRYinGO.2.go
Last active January 30, 2019 23:22
DRY in GO #2
func getEmployee(id string) (Employee, int) {
e, err := db.GetEmployeeByID(id)
if err != nil {
// error handling provided earlier
}
return e, 200
}
func GetEmployeeName(id string) (string, int) {
@cn007b
cn007b / DRYinGO.1.go
Last active January 30, 2019 23:22
DRY in GO #1
func GetEmployeeName(id string) (string, int) {
e, err := db.GetEmployeeByID(id)
if err != nil {
httpErrorCode := 500
switch err {
case ErrorInvalidID:
httpErrorCode = 400
case ErrorUnauthorized:
httpErrorCode = 401
case ErrorEmployeeNotFound:
@cn007b
cn007b / youtube 500
Created January 8, 2019 20:14
youtube error 500
APkpgMVe78Uwfds9i0eAaRtUZ1qoTfYpRShXjGjauvHtQBL_vAoXQH0b
q_2I29eK_8lxoiCauYd0JtgvSgmeGPz4HSkEHH418f0Ysl9txY-3WCcw
EZJsLUJXRvYjqLjd8tqWXtsfE45HrNN6akVsZwN3Rvil0UVGyCqq6dCP
QtGWOvZ4ka6zxb_rFvb4C0J5iY1tCgdQhAXD-sWyjQSU_prXgPSDOz7B
CSvBfsANNWtvqCxaQMnu3wFv2LgmbF0pu1Cam58XZjPH_m8rlhFbvXPV
gZ-iOmZFSJyTfb93boQGJ8fdQ6G0k7Z6hJLmyma4r7f6Y8kUwILiJ2he
S3jHXjdzO6e6b5L5buI6hnYcNEIPkxAwkEa7UktPeZI49X-PSBf2KY9R
0zjcdpQRYDbRLaNpKoyqjaKVkRn4O2DJimzskFS3S_IfQKlkRQEPxcux
HXcXH6Z-RqicpSfL-glePfLwSYAITzt1eo5Ls2lrBwkJuB8nXSj9uhNw
xHK7g6uXrTC3oPm5nAqGuzohZnkOqDgY5b0O_lPf6vu88InEpfgEtYeu
@cn007b
cn007b / gin.go
Created December 4, 2018 12:31
Plain Go vs Gin - gin
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/v1/id/:id", func(c *gin.Context) {
c.JSON(200, gin.H{
@cn007b
cn007b / plain.go
Created December 4, 2018 12:31
Plain Go vs Gin - plain go
package main
import (
"encoding/json"
"net/http"
"strings"
)
func main() {
http.HandleFunc("/v1/id/", func(w http.ResponseWriter, r *http.Request) {
@cn007b
cn007b / benchmark.plainGoVsGin.md
Last active December 18, 2023 10:51
Plain Go vs Gin

Plain Go vs Gin

The aim of this benchmark is to answer the question do you need framework for your tiny microservice.

Prerequisites

Let's consider simple example where we need framework only to parse URL (someone may decide that it's sufficient reason to start use framework for tiny microservice).

@cn007b
cn007b / Implementation2.service.go
Created November 25, 2018 23:22
eop - Implementation2 service
// service
func MustSignUp(username string) {
mustValidation(username)
mustSignUpFacebook(username)
mustSignUpTwitter(username)
mustSignUpPinterest(username)
}
func mustValidation(username string) {
@cn007b
cn007b / Implementation2.controller.go
Last active November 25, 2018 23:19
eop - Implementation2 controller
// controller
func SignUp(username string) {
defer recover.All(func(err interface{}) {
fmt.Printf("[pro_panic] SignUp: %s \n", err)
})
service.MustSignUp(username)
fmt.Printf("[pro_panic] SignUp: %s \n", "ok")
}
@cn007b
cn007b / recover.go
Created November 25, 2018 22:38
eop - thepkg/recover
// Performs recover in case of panic with error ErrorUsernameBlank
// otherwise panic won't be recovered and will be propagated.
defer recover.One(ErrorUsernameBlank, func(err interface{}) {
fmt.Printf("got error: %s", err)
})
// Performs recover in case of panic with error ErrorUsernameBlank or ErrorUsernameAlreadyTaken
// otherwise panic won't be recovered and will be propagated.
defer recover.Any([]error{ErrorUsernameBlank, ErrorUsernameAlreadyTaken}, func(err interface{}) {
fmt.Printf("got error: %s", err)