swap

package module
v0.0.0-...-949962e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2022 License: MIT Imports: 0 Imported by: 0

README

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

  1. Run go get github.com/brandondoesdev/slice-swap
  2. 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 ⭐

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SwapB

func SwapB(s []bool, i int, i1 int) []bool

func SwapF32

func SwapF32(s []float32, i int, i1 int) []float32

func SwapF64

func SwapF64(s []float64, i int, i1 int) []float64

func SwapI

func SwapI(s []int, i int, i1 int) []int

func SwapS

func SwapS(s []string, i int, i1 int) []string

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL