-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8d6a0a
commit 44b1544
Showing
3 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: Go | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: 1.18 | ||
|
||
- name: Build | ||
run: go build ./... | ||
|
||
- name: Run tests | ||
run: go test ./... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
MIT License | ||
|
||
Copyright (c) [year] [fullname] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
|
||
# Threadsafe | ||
|
||
A Go package providing thread-safe implementations of arrays, slices, and maps using generics and type constraints. | ||
|
||
## Installation | ||
|
||
To install the package, use the following command: | ||
|
||
```sh | ||
go get github.com/hayageek/threadsafe | ||
``` | ||
|
||
## Usage | ||
|
||
This package provides thread-safe implementations for arrays, slices, and maps. Below are usage examples for each of these data types. | ||
|
||
### Thread-Safe Array | ||
|
||
A thread-safe array with a fixed size. | ||
|
||
#### Example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yourusername/threadsafe" | ||
) | ||
|
||
func main() { | ||
// Create a new thread-safe array with size 5 | ||
arr := threadsafe.NewArray(5) | ||
|
||
// Set values in the array | ||
for i := 0; i < arr.Length(); i++ { | ||
arr.Set(i, i*10) | ||
} | ||
|
||
// Get values from the array | ||
for i := 0; i < arr.Length(); i++ { | ||
value, _ := arr.Get(i) | ||
fmt.Println(value) | ||
} | ||
} | ||
``` | ||
|
||
### Thread-Safe Slice | ||
|
||
A dynamically-sized, thread-safe slice. | ||
|
||
#### Example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yourusername/threadsafe" | ||
) | ||
|
||
func main() { | ||
// Create a new thread-safe slice | ||
slice := threadsafe.NewSlice[int]() | ||
|
||
// Append values to the slice | ||
for i := 0; i < 5; i++ { | ||
slice.Append(i * 10) | ||
} | ||
|
||
// Get values from the slice | ||
for i := 0; i < slice.Length(); i++ { | ||
value, _ := slice.Get(i) | ||
fmt.Println(value) | ||
} | ||
|
||
// Set values in the slice | ||
for i := 0; i < slice.Length(); i++ { | ||
slice.Set(i, i*20) | ||
} | ||
|
||
// Get updated values from the slice | ||
for i := 0; i < slice.Length(); i++ { | ||
value, _ := slice.Get(i) | ||
fmt.Println(value) | ||
} | ||
} | ||
``` | ||
|
||
### Thread-Safe Map | ||
|
||
A thread-safe map for storing key-value pairs. | ||
|
||
#### Example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yourusername/threadsafe" | ||
) | ||
|
||
func main() { | ||
// Create a new thread-safe map | ||
m := threadsafe.NewMap[string, int]() | ||
|
||
// Set values in the map | ||
m.Set("one", 1) | ||
m.Set("two", 2) | ||
m.Set("three", 3) | ||
|
||
// Get values from the map | ||
value, _ := m.Get("one") | ||
fmt.Println(value) | ||
|
||
value, _ = m.Get("two") | ||
fmt.Println(value) | ||
|
||
// Delete a key from the map | ||
m.Delete("two") | ||
|
||
// Check if a key exists | ||
_, ok := m.Get("two") | ||
if !ok { | ||
fmt.Println("Key 'two' not found") | ||
} | ||
|
||
// Get the length of the map | ||
length := m.Length() | ||
fmt.Println("Length:", length) | ||
|
||
// Get all keys from the map | ||
keys := m.Keys() | ||
fmt.Println("Keys:", keys) | ||
|
||
// Get all values from the map | ||
values := m.Values() | ||
fmt.Println("Values:", values) | ||
} | ||
``` |