4 ms·
Why? Let's say I have a table full of cars and have a method > func Car(id string) Car How do I know whether it fetched that from the database or created a ne
by throwaway2203 4y ago
Why? Let's say I have a table full of cars and have a method
> func Car(id string) Car
How do I know whether it fetched that from the database or created a new one and returned that to me?
- Thaxll 4y agoOnly the type Car matters, why would the function name tells you anything about mutation? It does not tells you if you receive *Car or Car or if it's a Car but pointers inside.
- notpushkin 4y agoBecause I want to know what function does?
- Thaxll 4y agoI think it's not possible to achieve with func name on its own, the closest thing I see would be comments on the function but again not reliable imo.
- throwaway2203 4y agoAbsolutely not reliable. Comments get stale faster than anything else in the codebase.
- wizhi 4y agoAssuming Car() is a receiver function, you should be able to infer "how" the Car is "fetched" based on the type receiving the function call. func (d CarDB) func Car() (Car, error) The above tells you everything you need to know. As for usage, again, the type and now also the variable names should let you infer everything you need. func f(db *CarDB) { c, _ := db.Car() } The function name makes as little of a guarantee as to the underlying "how" as these other factors.