slice-swap
Quickly swap two elements in a slice
Installation
Because of Golang's easy package system, you can install slice-swap in 2 steps
- Run
go get github.com/brandondoesdev/slice-swap
- Add
github.com/brandondoesdev/slice-swap
to your import statements
Usage
slice-swap has a function for every simple data type. (Multi-dimensional support coming soon)
SwapS(s []string, i int, i1 int) []string
- Swap function for string slices. s
is the slice, i
is the index of the first element, and i
is the index of the second element. Returns the edited slice.
SwapI(s []int, i int, i1 int) []int
- Swap function for integer slices. s
is the slice, i
is the index of the first element, and i
is the index of the second element. Returns the edited slice.
SwapF32(s []float32, i int, i1 int) []float32
- Swap function for float32 slices. s
is the slice, i
is the index of the first element, and i
is the index of the second element. Returns the edited slice.
SwapF64(s []float64, i int, i1 int) []float64
- Swap function for float64 slices. s
is the slice, i
is the index of the first element, and i
is the index of the second element. Returns the edited slice.
SwapB(s []bool, i int, i1 int) []bool
- Swap function for boolean slices. s
is the slice, i
is the index of the first element, and i
is the index of the second element. Returns the edited slice.
Example
Code:
package main
import (
"fmt"
"github.com/brandondoesdev/slice-swap"
)
func main() {
s := []int{1, 3, 2}
i := 1
i1 := 2
fmt.Println(s)
s = swap.SwapI(s, i, i1)
fmt.Println(s)
}
Terminal Output:
[1 3 2]
[1 2 3]
Make sure to star ⭐