Standard library / reflect
reflect.Swapper()
Returns a function that exchanges two indexed elements of the supplied slice; invalid indexes panic. Expected stdout: '[b a]\n'.
Formal Syntax & Structure
go syntaxgo
func Swapper(slice any) func(i, j int)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| slice | any | Slice whose elements will be swapped. |
Return Value
func(i, j int)
Returns a function that exchanges two indexed elements of the supplied slice; invalid indexes panic.
Working Example
Example Snippetgo
package main
import ( "fmt" "reflect")
func main() { s := []string{"a", "b"}; swap := reflect.Swapper(s); swap(0, 1); fmt.Println(s) }