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
refactor(context): refactor Keys
type to map[any]any
#3963
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
Keys
to map[any]any
Keys
type to map[any]any
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #3963 +/- ##
==========================================
- Coverage 99.21% 99.19% -0.03%
==========================================
Files 42 43 +1
Lines 3182 2722 -460
==========================================
- Hits 3157 2700 -457
+ Misses 17 12 -5
- Partials 8 10 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Why not just make it |
I think it's just like the It can be supported, so why not? Moreover, when the type customKey struct{}
ctx.Set(customKey{}, "xxx") package main
import (
"fmt"
"unsafe"
)
func main() {
var a int
var b string
var c struct{}
fmt.Println("a:", unsafe.Sizeof(a))
fmt.Println("b:", unsafe.Sizeof(b))
fmt.Println("c:", unsafe.Sizeof(c))
} output:
|
In Golang, maps can only have keys of types that are considered *comparable*.
This means the type needs to support the == and != operators for comparison.
which means that structs have to have nothing but *comparable* types as
their fields.
it introduces confusion when someone tries to use a key that is not
*comparable.*
*comparable *is broken as far as I am concerned, not being able to provide
a function that makes any struct *comparable* is a serious oversight and
flaw with the language.
I have ended up having to write a library that generates a unique hash of
any struct so that I can pass an array of structs I want to use as keys and
array of values and generate a map with consistent hashing of the identity
(fingerprint) of a struct as a key and the struct as the value. This allows
me to generate keys from structs and look them up in the map without having
to worry about if something I want to store in a map key is a valid key or
not.
…On Tue, May 14, 2024 at 11:24 AM Flc゛ ***@***.***> wrote:
Why not just make it comparable, or just any?
I think it's just like the key in context.WithValue(ctx, key, value).
It can be supported, so why not?
Moreover, when the key is set to struct{}, it saves more memory, ex:
type customKey struct{}
ctx.Set(customKey{}, "xxx")
—
Reply to this email directly, view it on GitHub
<#3963 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABF7754OZ67JDDN5LWVVBDZCIUERAVCNFSM6AAAAABHQT7FSWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJQGUZDINBYHA>
.
You are receiving this because you commented.Message ID:
***@***.***>
--
Jarrod Roberson
678.551.2852
|
Keys
to support more types.The first step in solving #1123 (comment)