3 ms·
Soon after generics were introduced, I put a generic Maybe type in our team's codebase. Its been very helpful. Other than that though, I would say pointers are
by liampulles 3y ago
Soon after generics were introduced, I put a generic Maybe type in our team's codebase. Its been very helpful.
Other than that though, I would say pointers are a last resort for indicating optional (I certainly do use them for this, but uncommonly). Idiomatic Go I think would suggest you use the empty value (I would use an empty value plus an // Optional comment).
Using pointers in general is fairly uncommon in the Go code I write - I don't often need the mutability as you say, because I try to stick to pure functions.
My thinking also, regardless of the language: be suspicious of a function that accepts an optional type. One should ask: is there a non short-circuit or non-error path in the empty case? If not, don't pass an optional, unwrap up the chain. Even if there is a valid path for an empty value, perhaps ask if the function should be split into two cleaner functions.