Skip to content

Instantly share code, notes, and snippets.

@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: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: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 / 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 / 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_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.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 / 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 / handler.go
Created February 1, 2022 10:03
Example for blog post: Go bits: Magic with functions
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
@AntonStoeckl
AntonStoeckl / functionsandinterfaces.go
Created February 1, 2022 10:06
Example for blog post: Go bits: Magic with functions
package functionsandinterfaces
type ForRegisteringCustomers interface {
Register(name, emailAddress string) error
}
type ForRegisteringCustomersFunc func(name, emailAddress string) error
// Register is a receiver method for the ForRegisteringCustomersFunc
// type that fullfills the ForRegisteringCustomers interface