Created
February 24, 2023 08:44
-
-
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.
This file contains 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 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