magnify_slice.go 508 B

12345678910111213141516171819202122232425
  1. package main
  2. import "fmt"
  3. var s []int
  4. func main() {
  5. s = []int{1, 2, 3}
  6. fmt.Println("The length of s before enlarging is:", len(s))
  7. fmt.Println(s)
  8. s = enlarge(s, 5)
  9. fmt.Println("The length of s after enlarging is:", len(s))
  10. fmt.Println(s)
  11. }
  12. func enlarge(s []int, factor int) []int {
  13. ns := make([]int, len(s)*factor)
  14. // fmt.Println("The length of ns is:", len(ns))
  15. copy(ns, s)
  16. //fmt.Println(ns)
  17. s = ns
  18. //fmt.Println(s)
  19. //fmt.Println("The length of s after enlarging is:", len(s))
  20. return s
  21. }