
A much better solution is to define different types and use them when representing different things!
Imagine if, instead of using plain old UUIDs, each of your models defined its own ID type:type AccountID uuid.UUID type UserID uuid.UUID func UUIDTypeMixup() { { userID := UserID(uuid.New()) DeleteUser(userID) // no error } { accountID := AccountID(uuid.New()) DeleteUser(accountID) // ^ error: Cannot use 'accountID' (type AccountID) as the type UserID } { accountID := uuid.New() DeleteUserUntyped(accountID) // no error at compile time; likely error at runtime } }In libwxI previously discussed this in my 2015 talk, "String is not a sufficient type."
ConclusionYour type system is there to help you.
Your models should each have their own ID type.
Meanwhile, it's simple to set up types that entirely eliminate this class of bug, even in a language like Go that isn't known for having a particularly powerful type system.
1 week, 3 days ago: Hacker News: Front Page