Skip to content

Instantly share code, notes, and snippets.

View napicella's full-sized avatar

Nicola Apicella napicella

View GitHub Profile
@napicella
napicella / chain.go
Last active April 1, 2018 14:36
Golang-patterns Chain example
package chain
import "fmt"
func ExampleChain() {
endpoint, _ := chain(
loadEndpointFromConfigFile,
loadEndpointFromEnvVariables,
loadEndpointFromDatabase,
).get()
@napicella
napicella / constants.go
Created April 1, 2018 14:37
Golang-patterns constants example
// Package constants shows a pattern to group constants together
package constants
// Endpoint contains the endpoint configuration
var Endpoint struct {
Hostname string
Port int
}
func init() {
@napicella
napicella / maybe_test.go
Last active April 1, 2018 14:43
Golang-patterns maybe
Context("User present", func() {
var greeting string
MaybeUser(getUser(1)).IfPresent(func(u *User) {
greeting = "Hello " + u.name
})
It("greets the user", func() {
Expect(greeting).To(Equal("Hello Mickey"))
})
})
@napicella
napicella / options_test.go
Created April 1, 2018 14:43
Golang-patterns options
Context("Greeting with no Name option", func() {
It("returns default greeting", func() {
greeting := NewGreeting()
Expect(greeting.get()).To(Equal("Hello Stranger"))
})
})
Context("Greeting with Name option", func() {
It("returns custom greeting", func() {
greeting := NewGreeting(Name("Mickey"))
@napicella
napicella / functiontype.go
Created April 1, 2018 16:23
Golang-patterns function type
package functiontype
import "fmt"
type Greeting func(name string) string
func GreetingService(request Request, greeting Greeting) string {
return fmt.Sprintf("Service says: %s", greeting(request.user))
}
@napicella
napicella / main.go
Created February 29, 2020 17:50
Companion code for the Linux terminals blog series
// Companion code for the Linux terminals blog series: https://dev.to/napicella/linux-terminals-tty-pty-and-shell-192e
// I have simplified the code to highlight the interesting bits for the purpose of the blog post:
// - windows resizing is not addressed
// - client does not catch signals (CTRL + C, etc.) to gracefully close the tcp connection
//
// Build: go build -o remote main.go
// In one terminal run: ./remote -server
// In another terminal run: ./remote
//
// Run on multiple machines:
package main
import (
"context"
"net/http"
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/prozz/aws-embedded-metrics-golang/emf"
)
package main
import (
"context"
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/prozz/aws-embedded-metrics-golang/emf"