SyncMap
is a type-safe and generic wrapper around Go's sync.Map
. It simplifies the use of sync.Map
by allowing you to define the types for both keys and values.
It makes using sync.Map
easier and safer by letting you define the types for keys and values when you create the sync.Map
.
Using SyncMap
has several advantages:
- Generic Type: Helps avoid type mismatches during runtime.
- Clearer Code: No need for type assertions from
interface{}
. - Simple Usage: Works just same as
sync.Map
, same methods.
go get github.com/yyle88/syncmap
Here’s a simple example showing how you can use SyncMap
to safely store and retrieve structured data.
package main
import (
"fmt"
"github.com/yyle88/syncmap"
)
type User struct {
Name string
Age int
}
func main() {
// Create a SyncMap with string keys and User values
users := syncmap.NewMap[string, *User]()
// Add a user to the map
users.Store("u1", &User{Name: "Alice", Age: 30})
// Retrieve the user
if user, ok := users.Load("u1"); ok {
fmt.Printf("User: Name: %s, Age: %d\n", user.Name, user.Age) // Output: User: Name: Alice, Age: 30
}
}
- Defines Clear Types: The key is a
string
, and the value is aUser
struct pointer, avoiding the use ofinterface{}
for values. - Simplifies Data Access: No need for manual type assertions like
user = v.(*User)
when retrieving data. - Works Same with
sync.Map
: Functions likeStore
andLoad
are exactly the same as insync.Map
, so you don't need to learn new methods.
package main
import (
"fmt"
"github.com/yyle88/syncmap"
)
type Person struct {
Name string
Age int
HomePage string
}
func main() {
// Create a SyncMap with int keys and *Person values
mp := syncmap.NewMap[int, *Person]()
// Add some persons to the map
mp.Store(1, &Person{
Name: "Kratos",
HomePage: "https://go-kratos.dev/",
})
mp.Store(2, &Person{
Name: "YangYiLe",
Age: 18,
})
mp.Store(3, &Person{
Name: "DiLiReBa",
Age: 18,
})
// Delete the entry with key 3
mp.Delete(3)
// Iterate over all items in the map
mp.Range(func(key int, value *Person) bool {
fmt.Println(key, value.Name, value.Age, value.HomePage)
return true
})
}
- Store and Delete: This example shows how to add multiple entries and delete one using the
Delete
method. - Iterate Over Data: The
Range
method is used to iterate over all the key-value pairs in the map, which simplifies traversing the map compared to manually managingsync.Map
's iteration. - Simplified Operations: Just like with the previous example, no type assertions are required, and the usage is straightforward.
SyncMap
provides the following functions:
Function | Description |
---|---|
Store(key, value) |
Adds or updates a key-value. |
Load(key) |
Retrieves the value. |
LoadOrStore(key, value) |
Returns the value if it exists; otherwise, adds a new key-value. |
Delete(key) |
Removes the key-value pair from the map. |
Range(func) |
Iterates over all key-value pairs in the map. |
Same as sync.Map
.
This project is open-source under the MIT License. You can find the full text of the license in the LICENSE file.
We welcome contributions of all kinds! Whether it’s reporting a bug, suggesting a feature, or submitting code improvements.
If you find this package valuable, give it a star on GitHub! Thank you!!!