Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created February 24, 2023 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/2e9bbbc442e9ad73daf4286cdb52eebb to your computer and use it in GitHub Desktop.
Save JoshCheek/2e9bbbc442e9ad73daf4286cdb52eebb to your computer and use it in GitHub Desktop.
Golang's type system failing me, tests passed the values, but prod passed pointers to them, the type system didn't care, but staging broke b/c of a type error.
package main
import (
"fmt"
)
type Objs []Obj
type Obj interface{ GetType() string }
type IntObj struct{ Int int }
type StrObj struct{ Str string }
func (i IntObj) GetType() string { return "int" }
func (s StrObj) GetType() string { return "str" }
func main() {
i := IntObj{Int: 12}
s := StrObj{Str: "omg"}
omg(Objs{i, &i, s, &s})
}
func omg(objs Objs) {
for _, obj := range objs {
if intObj, ok := obj.(IntObj); ok {
fmt.Printf("IntObj: %s, %d\n", intObj.GetType(), intObj.Int)
} else if strObj, ok := obj.(StrObj); ok {
fmt.Printf("StrObj: %s, %s\n", strObj.GetType(), strObj.Str)
} else {
fmt.Printf("Unknown: %s\n", obj.GetType())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment