Skip to content

Instantly share code, notes, and snippets.

@AntonStoeckl
AntonStoeckl / handlerfunc.go
Created February 1, 2022 10:02
Example for blog post: Go bits: Magic with functions
// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)
// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
f(w, r)
}
@AntonStoeckl
AntonStoeckl / closures.go
Created February 1, 2022 09:58
Example for blog post: Go bits: Magic with functions
package closures
// CustomerService has repo as a protperty
// that will be "enclosed" in the receiver methods
type CustomerService struct {
repo CustomerRepository
}
// Register is a receiver method that will be used as a closure
func (s *CustomerService) Register(customerName string) error {
@AntonStoeckl
AntonStoeckl / closures_test.go
Created February 1, 2022 09:56
Example for blog post: Go bits: Magic with functions
package closures_test
import (
"testing"
"github.com/matryer/is"
. "go-bits/magic-with-functions/closures"
)
@AntonStoeckl
AntonStoeckl / closures_test.go
Created February 1, 2022 09:54
Example for blog post: Go bits: Magic with functions
package closures_test
import (
"testing"
"github.com/matryer/is"
. "go-bits/magic-with-functions/closures"
)
@AntonStoeckl
AntonStoeckl / closures.go
Created February 1, 2022 09:53
Example for blog post: Go bits: Magic with functions
package closures
// IsBiggerThan returns a closure function that is parametrized with the
// first parameter
func IsBiggerThan(first int) func(second int) bool {
isBigger := func(second int) bool {
return second > first
}
return isBigger
@AntonStoeckl
AntonStoeckl / basics_test.go
Created February 1, 2022 09:49
Example for blog post: Go bits: Magic with functions
package basics_test
import (
"testing"
"github.com/matryer/is"
. "go-bits/magic-with-functions/basics"
)
@AntonStoeckl
AntonStoeckl / basics.go
Created February 1, 2022 09:45
Example for blog post: Go bits: Magic with functions
package basics
// CompareFn is a function type
type CompareFn func(first, second int) bool
// OtherCompare is a higher order function that compares two integers via a
// function that is passed in as the third parameter using a function type
// annotation, so the function type is used to annotate the type of the
// parameter
func OtherCompare(first, second int, compareFn CompareFn) bool {
@AntonStoeckl
AntonStoeckl / basics_test.go
Created February 1, 2022 09:37
Example for blog post: Go bits: Magic with functions
package basics_test
import (
"testing"
"github.com/matryer/is"
. "go-bits/magic-with-functions/basics"
)
@AntonStoeckl
AntonStoeckl / basics.go
Created February 1, 2022 09:36
Example for blog post: Go bits: Magic with functions
package basics
// IsBigger is a plain function that compares two integers
// and return true if the first is bigger than the second ;-)
func IsBigger(first, second int) bool {
return first > second
}
// Compare is a higher order function that compares two integers via a function
// that is passed in as the third parameter using explicit annotation,
@AntonStoeckl
AntonStoeckl / http_hander_impl.go
Created January 19, 2022 16:28
Example for blog post Go bits: Variable names
func (h *SomeHandler) (w ResponseWriter, r *Request) {
// ...
}