Golang中传递切片给函数的正确方式

223 阅读1分钟
package main

import "fmt"

func main() {
	s := []string{"A", "B", "C"}
	fmt.Printf("  原始切片:%v \n",s)
	sliceOpt(&s)
	fmt.Printf("修改后切片:%v \n",s)
}

func sliceOpt(s *[]string) {
	*s = append(*s, "D")
	(*s)[0] = "E"
}

输出结果:

  原始切片:[A B C] 
修改后切片:[E B C D] 

Process finished with the exit code 0