New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: database/sql: allow rows.Scan to create value, not just assign #20549
Comments
/cc @kardianos |
Could you provide an example please? |
@kardianos the file attachment in my original post has more detail. The TLDR version:
I suspect a lot of people use the latter implementation to avoid returning the nil pointer error but I think a more practical solution might be for convertAssign to allocate nil pointers instead of returning an error. The attachment in my original post also has some code for types that implement the Scanner interface. |
Thank you for your clarification. I'm unsure if that's what we want to do or not. The first step would be to see how involved this change would be and what the runtime cost would be to do this. |
If you have
and call
there is just no way for It seems like that's what you're asking Scan to do here. Am I missing something? |
@rsc , yes you are correct. There is nothing we can do here in this case. I was not thinking correctly when I posted my last comment. |
@rsc, @kardianos, Thanks for the clarification. Could anything be added to the documentation to clarify behavior when dest is a pointer type? We end up with inconsistent client side code when developers randomly mix I admit I don't understand lower level implementation well but perhaps something like "If dest is a nil-pointer, Scan returns sql.errNilPtr. If dest is a pointer to a pointer, Scan attempts to indirect dest." |
@220kts The issue is a bit more fundamental then that. |
@kardianos Yes, thanks. The part we struggle with - due to limited understanding, we're application developers - is inconsistent client side code. Often our developers know in a certain context This discussion clarified the issue for me personally but perhaps some additional verbiage or an example with pointer types in the documentation might be useful for other developers as well? |
I don't think there is anything to really add on database/sql side. Though I could be wrong. I think this might be just better understanding points. It is tempting to use a pointer as a NULLABLE value in Go. I've done it myself. However, when at all possible I actually try to avoid using pointers to facilitate database NULL values. Database NULL and Go nil arguably convey different concepts: Go nil / pointers point to another value and remove the value from the current struct. The database NULL says the value is not be present. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go1.8.1 linux/amd64
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
What did you do?
See attached file
main.txt
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
What did you expect to see?
What did you see instead?
Would it be possible for the Scan() function to allocate nil pointers instead of returning an error? Alternatively, should it be documented what the intended behavior is if the client passes the address of a pointer variable. I suspect a lot of people pass the address of pointer variables to get around the nil pointer error (we do). Allocating pointers would make Scan() behave similar to json.Unmarshal(), which is quite practical in non-trivial applications where the Scan() destination is often a pointer inside a struct.
The text was updated successfully, but these errors were encountered: