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
if nilCommand != nil { | |
_ = nilCommand.CommandType() | |
} |
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 commandhandling | |
// ConfirmEmailAddress implements the Command interface | |
type ConfirmEmailAddress struct { | |
commandType string | |
// some useful properties | |
} | |
func NewConfirmEmailAddress() *ConfirmEmailAddress { | |
return &ConfirmEmailAddress{ |
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 commandhandling | |
import ( | |
"errors" | |
) | |
var ErrCommandIsNilInterface = errors.New("command is nil interface") | |
var ErrCommandIsNilPointer = errors.New("command is nil pointer") | |
var ErrUnknownCommand = errors.New("unknown command received") |
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 commandhandling_test | |
import ( | |
"testing" | |
"github.com/matryer/is" | |
"go-bits/interfaces-and-nil/commandhandling" | |
) |
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 factorial | |
func PositiveFactorial(number uint8) uint { | |
if number == 0 { | |
return 1 | |
} | |
result := uint(1) | |
for n := uint8(1); n <= number; n++ { |
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 cars | |
import ( | |
"errors" | |
) | |
// For the sake of this example I simply use alias types for the of Car | |
type ID string | |
type CarMake string |
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 (c *Cars) FilterSportscarsV2() []*Car { | |
var sportscars []*Car | |
for id, car := range c.items { | |
hasEnoughHorsepower := car.Horsepower > sportscarsMinHorsepower | |
hasSportyMake := car.CarMake == sportscarsMake | |
hasASportyColor := car.Color == sportscarsColor | |
if hasEnoughHorsepower && hasSportyMake && hasASportyColor { | |
sportscars = append(sportscars, car) |
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
type Handler interface { | |
ServeHTTP(ResponseWriter, *Request) | |
} |
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) { | |
// ... | |
} |
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, |