https://github.com/zzwx/splice
go1.18 generics implementation of JavaScript's array.splice function for []T where T is constrained to any
https://github.com/zzwx/splice
generics go go118
Last synced: about 2 months ago
JSON representation
go1.18 generics implementation of JavaScript's array.splice function for []T where T is constrained to any
- Host: GitHub
- URL: https://github.com/zzwx/splice
- Owner: zzwx
- License: mit
- Created: 2020-11-24T18:03:32.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-04-27T14:14:26.000Z (about 3 years ago)
- Last Synced: 2025-02-12T17:17:10.502Z (3 months ago)
- Topics: generics, go, go118
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pkg.go.dev/github.com/zzwx/splice/v2)
# Splice
Package splice is a `go1.18` generics implementation of JavaScript's `array.splice` function for `[]T` where `T` is constrained to `any`.
## v1
For a non-generic string-only version use [import "github.com/zzwx/splice"](https://pkg.go.dev/github.com/zzwx/splice).
# Example
```go
import "github.com/zzwx/splice/v2"var months = []string{"Jan", "March", "April", "June"}
splice.Splice(&months, 1, 0, "Feb") // inserts at index 1
fmt.Println(months)
deleted := splice.Splice(&months, 4, 1, "May") // replaces 1 element at index 4
fmt.Println(months)
fmt.Println(deleted)
// Output:
// [Jan Feb March April June]
// [Jan Feb March April May]
// [June]
```