-
-
Notifications
You must be signed in to change notification settings - Fork 242
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
Generics support #67
Comments
I think it's matter of time for community to be able to share useful feedback. Until 1.18 become stable I would suspect we wont have good perspective. |
Interesting! :) |
Looks like we will have an official generic |
Hi guys, here's the implementation of go null with generics: https://github.com/lomsa-dev/gonull usage: package main
import (
"encoding/json"
"fmt"
"github.com/lomsa-dev/gonull"
)
type MyCustomInt int
type MyCustomFloat32 float32
type Person struct {
Name string
Age gonull.Nullable[MyCustomInt]
Address gonull.Nullable[string]
Height gonull.Nullable[MyCustomFloat32]
}
func main() {
jsonData := []byte(`{"Name":"Alice","Age":15,"Address":null,"Height":null}`)
var person Person
err := json.Unmarshal(jsonData, &person)
if err != nil {
panic(err)
}
fmt.Printf("Unmarshalled Person: %+v\n", person)
marshalledData, err := json.Marshal(person)
if err != nil {
panic(err)
}
fmt.Printf("Marshalled JSON: %s\n", string(marshalledData))
} |
|
Nice, I will look into a new release that includes support for that and the other types we're missing from the stdlib like uint |
Released in v5 https://github.com/guregu/null/releases/tag/v5.0.0 |
Any ideas or opinions on how to handle generics in this library?
I played around with creating a generic
Null[T]
type, but I wonder if it's even useful at that point, because we are essentially recreating the Optional type you might find in other languages. At what point is it going too far?Maybe ideally we could make generic Null/Zero types and use the materialized version of it for each pre-existing type.
I'm also curious if we'll see a generic
sql.Null[T]
type in the standard library, which might obsolete a good chunk of this library, especially if this change lands: golang/go#5901 (which lets you easily specify custom marshalers for JSON, moving the responsibility from the object itself to the encoding site).See also: golang/go#48702
The text was updated successfully, but these errors were encountered: