Skip to content

Instantly share code, notes, and snippets.

@AntonStoeckl
AntonStoeckl / snippet.go
Last active January 14, 2022 20:12
Example for blog post Go bits: Interfaces and Nil Pointers
if nilCommand != nil {
_ = nilCommand.CommandType()
}
@AntonStoeckl
AntonStoeckl / confirm_email_address.go
Created January 14, 2022 20:14
Example for blog post Go bits: Interfaces and Nil Pointers
package commandhandling
// ConfirmEmailAddress implements the Command interface
type ConfirmEmailAddress struct {
commandType string
// some useful properties
}
func NewConfirmEmailAddress() *ConfirmEmailAddress {
return &ConfirmEmailAddress{
@AntonStoeckl
AntonStoeckl / command_handler.go
Created January 14, 2022 20:15
Example for blog post Go bits: Interfaces and Nil Pointers
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")
@AntonStoeckl
AntonStoeckl / command_handler_test.go
Last active January 14, 2022 20:26
Example for blog post Go bits: Interfaces and Nil Pointers
package commandhandling_test
import (
"testing"
"github.com/matryer/is"
"go-bits/interfaces-and-nil/commandhandling"
)
@AntonStoeckl
AntonStoeckl / factorial.go
Created January 19, 2022 16:15
Example for blog post Go bits: Variable names
package factorial
func PositiveFactorial(number uint8) uint {
if number == 0 {
return 1
}
result := uint(1)
for n := uint8(1); n <= number; n++ {
@AntonStoeckl
AntonStoeckl / cars.go
Last active January 19, 2022 16:21
Example for blog post Go bits: Variable names
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
@AntonStoeckl
AntonStoeckl / filter_sportscars_v2.go
Created January 19, 2022 16:24
Example for blog post Go bits: Variable names
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)
@AntonStoeckl
AntonStoeckl / http_handler_interface.go
Created January 19, 2022 16:27
Example for blog post Go bits: Variable names
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
@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) {
// ...
}
@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,