This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package closures_test | |
import ( | |
"testing" | |
"github.com/matryer/is" | |
. "go-bits/magic-with-functions/closures" | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package closures_test | |
import ( | |
"testing" | |
"github.com/matryer/is" | |
. "go-bits/magic-with-functions/closures" | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package basics_test | |
import ( | |
"testing" | |
"github.com/matryer/is" | |
. "go-bits/magic-with-functions/basics" | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package basics_test | |
import ( | |
"testing" | |
"github.com/matryer/is" | |
. "go-bits/magic-with-functions/basics" | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (h *SomeHandler) (w ResponseWriter, r *Request) { | |
// ... | |
} |