diff options
| author | a73x <[email protected]> | 2024-08-31 12:51:45 +0100 |
|---|---|---|
| committer | a73x <[email protected]> | 2024-08-31 12:51:45 +0100 |
| commit | c3dbbd0e244f5e70460af3283e21807b6dccd39f (patch) | |
| tree | 3a4ec395ea0931801b3362c24ccb305ae31ee2d1 | |
| parent | c161efdbe8deaaac3c8cd338dad1fd3f6c87724a (diff) | |
post 5
| -rw-r--r-- | content/posts/005.md | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/content/posts/005.md b/content/posts/005.md new file mode 100644 index 0000000..55aa9ea --- /dev/null +++ b/content/posts/005.md @@ -0,0 +1,40 @@ +--- +title: "Go's unique pkg" +tags: posts +--- + +https://pkg.go.dev/unique +>The unique package provides facilities for canonicalising ("interning") comparable values. [[1](https://pkg.go.dev/unique)] + +oh yeah, thats obvious I fully understand what this package does now, great read, tune in for the next post. + +Interning, is the re-use of an object of equal value instead of creating a new one. I'm pretending I knew this but really I've just reworded [Interning](https://en.wikipedia.org/wiki/Interning_(computer_science)). + +So lets try again. + +If you're parsing out a csv of bank transactions, its very likely a lot of names will be repeated. Instead of allocating memory for each string representing a merchant, you can simply reuse the the same string. + +So the dumbed down version might look like +```go +var internedStrings sync.Map + +func Intern(s string) string { + if val, ok := internedStrings.Load(s); ok { + return val.(string) + } + internedStrings.Store(s, s) + return s +} +``` +With a small demo here https://go.dev/play/p/piSYjCHIcLr + +This implementation is fairly naive, it can only grow and it only works with strings, so naturally go's implementation is a better. + +It's also worth noting, that since strings are a pointer under the hood +>When comparing two strings, if the pointers are not equal, then we must compare their contents to determine equality. But if we know that two strings are canonicalized, then it _is_ sufficient to just check their pointers. [[2](https://go.dev/blog/unique)] + +So to recap, goes `unique` package provides a way to reuse objects instead of creating new ones, if we consider the objects of equal value. + +## References +1. https://pkg.go.dev/unique +2. https://go.dev/blog/unique
\ No newline at end of file |
