golang 数组倒序

实例如下:

package main

import (
    "fmt"
)

//数组倒序函数
func Reverse(arr *[]string) {
	var temp string
	length := len(*arr)
	for i := 0; i < length/2; i++ {
		temp = (*arr)[i]
		(*arr)[i] = (*arr)[length-1-i]
		(*arr)[length-1-i] = temp
	}
}


func main() {
    s := []string{"a", "b", "c"}
    Reverse(&s)
    fmt.Println(s)
}

输出结果:

[c b a]

 

 

你可能感兴趣的:(golang 数组倒序)