Safe and structured concurrency primitives for Go, inspired by Rust's ownership and sync model.
- 🦀 Rust-Inspired Safety: Memory-safe concurrency patterns inspired by Rust's Arc, Mutex, OnceCell
- 🔒 Thread-Safe by Design: All operations guarantee safe concurrent access
- 🚀 Go 1.24 Optimized: Leverages latest Go features like
atomic.Pointer[T]andmaps.Clone - 🛡️ No Raw Access: Controlled API prevents data races and memory corruption
- 📦 Zero Dependencies: Pure Go implementation with no external dependencies
- 🧪 Well Tested: Comprehensive test suite with >90% coverage
- 🎯 Production Ready: Battle-tested concurrency primitives
go get github.com/Gosayram/gokoncurentpackage main
import (
"fmt"
"github.com/Gosayram/gokoncurent"
)
func main() {
// Arc[T] - Atomic Reference Counting
data := gokoncurent.NewArc("Hello, World!")
clone := data.Clone()
fmt.Println(*data.Get()) // "Hello, World!"
// ArcMutex[T] - Safe shared mutable state
counter := gokoncurent.NewArcMutex(0)
counter.WithLock(func(value *int) {
*value += 1
})
// RWArcMutex[T] - Read-write mutex
rwCounter := gokoncurent.NewRWArcMutex(0)
rwCounter.WithRLock(func(value *int) { fmt.Println(*value) })
rwCounter.WithLock(func(value *int) { *value += 1 })
// CondVar - Conditional variables
cv := gokoncurent.NewCondVar()
go func() { cv.Wait(); fmt.Println("Signaled!") }()
cv.Signal()
// Barrier - Synchronization for multiple goroutines
b := gokoncurent.NewBarrier(3)
go func() { b.Wait(); fmt.Println("All workers synchronized!") }()
// OnceCell[T] - Lazy initialization
cell := gokoncurent.NewOnceCell[string]()
cell.Set("initialized once")
value, ok := cell.Get()
fmt.Println(value, ok) // "initialized once", true
}GoKoncurent provides safe concurrency primitives organized in phases:
- Thread-safe reference counting using
atomic.Int64 - Rust-like
Clone()method for sharing between goroutines - Automatic resource cleanup when reference count reaches zero
- Safe
Get()access without raw pointer exposure
- Wrapper around
sync.Mutexwith safe API - Access only through
WithLock(func(*T))to prevent deadlocks - Built on Arc[T] for safe sharing between goroutines
- Thread-safe read-write mutex for shared mutable state
WithRLock(func(*T))for read access,WithLock(func(*T))for write access- Optimized for scenarios with more reads than writes
- Conditional variables for goroutine coordination
- Similar to
sync.Condbut with atomic reference counting - Support for context cancellation and timeouts
- Convenience functions
Notify()andNotifyBroadcast()
- Synchronization primitive for waiting for multiple goroutines
- Atomic reference counting with safe cleanup
- Support for barrier reset and multiple cycles
- Thread-safe coordination of N goroutines
- Rust-like OnceCell/Lazy equivalent
- Uses
sync.Onceandatomic.Pointer[T]from Go 1.24 Set(value T)andGet() (T, bool)methods
- Race-free map operations with
sync.RWMutex - Utilizes
maps.Clonefrom Go 1.24 - Snapshot and iteration support without data races
- Simplified API for managing N goroutines
TaskPool.Run(ctx, func())with context controlFuture[T]for async result handling
| Feature | Usage |
|---|---|
atomic.Pointer[T] |
OnceCell and Arc implementations |
maps.Clone, maps.Equal |
SafeMap operations |
slices.Compact, slices.Delete |
Future SafeSlice support |
Generic sync.Pool |
Arc[T] and Future[T] allocation |
| Enhanced compile errors | Early detection of unsafe access patterns |
- Go 1.24 or later
- No external dependencies
Full API documentation is available at GoDoc.
See the examples directory for comprehensive usage examples, including:
basic/– minimal one-file examples for each primitiveadvanced_usage.go– full demonstration combining all primitivesadvanced/map_slice_example– safe concurrent manipulation of maps & slices withArcMutexadvanced/oncecell_error_example– robust error handling withOnceCellretries
Tip: run
go test -race ./...to execute the stress-test suite we added in vNext. It exercises every primitive under heavy concurrency to guarantee race-free operation.
We welcome contributions! Please see CONTRIBUTING.md for details.
-
Clone the repository:
git clone https://github.com/Gosayram/gokoncurent.git cd gokoncurent -
Install development tools:
go get -tool github.com/golangci/golangci-lint/cmd/golangci-lint
-
Run tests:
go test ./... -
Run linter:
go tool golangci-lint run
- All code must pass
go fmt,go vet, andgolangci-lint - Test coverage must be maintained at >90%
- All public APIs must be documented
- Follow semantic versioning for releases
This project uses Semantic Versioning. For the versions available, see the tags on this repository.
See CHANGELOG.md for a detailed history of changes.
This project is licensed under the MIT License - see the LICENSE file for details.
- 📖 Documentation
- 🐛 Issues
- 💬 Discussions
- Built with Go 1.24
- Inspired by Rust's concurrency model
- Thanks to all contributors
Made with ❤️ for safe Go concurrency