点击运行
package main import ( "fmt" ) func countries() []string { countries := []string{"USA", "Singapore", "Germany", "India", "Australia"} neededCountries := countries[:len(countries)-2] countriesCpy := make([]string, len(neededCountries)) copy(countriesCpy, neededCountries) //将neededCountries 拷贝到 countriesCpy return countriesCpy } func main() { countriesNeeded := countries() fmt.Println(countriesNeeded) }
运行结果 :
正在执行...