golang切片反转

切片反转

Posted by 果果 on November 24, 2022

golang切片反转

package main

import "fmt"

func reverse(s []int) []int {
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		s[i], s[j] = s[j], s[i]
	}
	return s
}

func main() {
	s1 := []int{1, 2, 3, 4, 5}
	s2 := reverse(s1)
	fmt.Println(s2)
}