Standard library / sort
sort.IntSlice.Swap()
Exchanges elements at two valid indices in the receiver; invalid indices panic. Expected stdout: '[2 1 3]\n'.
Formal Syntax & Structure
go syntaxgo
func (x IntSlice) Swap(i, j int)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| i | int | Index or start position. |
| j | int | Second index or exclusive end position. |
Working Example
Example Snippetgo
package main
import ( "fmt" "sort")
func main() { a := sort.IntSlice([]int{1, 2, 3}) a.Swap(0, 1) fmt.Println(a)}