A cross-platform Go library for creating and managing shared memory objects.
- Cross-platform support (Linux, Unix, Windows)
go get github.com/eslizn/shmpackage main
import (
"fmt"
"github.com/eslizn/shm"
)
type Data struct {
Value int
}
func main() {
// Create a new shared memory object
obj, err := shm.New[Data]()
if err != nil {
panic(err)
}
defer obj.Close()
// Access the shared data
obj.Value = 42
fmt.Println("Value:", obj.Value)
// Reset the memory to zero values
shm.Memset(obj)
if err != nil {
panic(err)
}
}Creates or opens a shared memory object of type T.
Options:
WithName(name string)- Specifies a custom name for the shared memory mapping fileWithFinder(finder Finder)- Specifies a custom file finder
Resets the shared memory object to its zero value.
Releases the shared memory object.
Returns the size in bytes of type T.
- Linux: Uses
/dev/shmdirectory - Unix: Uses system temp directory
- Windows: Uses Windows file mapping API
You can customize the shared memory location:
// Custom name
obj, err := shm.New[Data](shm.WithName("my-shared-data"))
// Custom finder
finder := func(name string) string {
return filepath.Join("/custom/path", name)
}
obj, err := shm.New[Data](shm.WithFinder(finder))Contributions are welcome! Please open an issue or submit a pull request.
MIT