Skip to content

Instantly share code, notes, and snippets.

@jreisinger
Last active January 11, 2020 12:11
Show Gist options
  • Select an option

  • Save jreisinger/1d283d111a8f82e6529f34e8e934657c to your computer and use it in GitHub Desktop.

Select an option

Save jreisinger/1d283d111a8f82e6529f34e8e934657c to your computer and use it in GitHub Desktop.
package main
import "fmt"
// printArray prints type, elements' values, capacity and length of an array
func printArray(a [3]int) {
fmt.Printf("%T\t", a)
for _, v := range a {
fmt.Printf("%d ", v)
}
fmt.Printf("\tCapacity: %d, Length: %d\n", cap(a), len(a))
}
// printSlice prints type, elements' values, capacity and length of a slice
func printSlice(s []int) {
fmt.Printf("%T\t", s)
for _, v := range s {
fmt.Printf("%d ", v)
}
fmt.Printf("\tCapacity: %d, Length: %d\n", cap(s), len(s))
}
func main() {
a := [...]int{1,2,3} // array
s := []int{1,2,3} // slice
// I created different functions for printing info on arrays and slices
// since they are different types ...
printArray(a)
printSlice(s)
// You can't do this with arrays since they're not dynamic ...
s = append(s, -100)
printSlice(s)
s = append(s, 42, 43)
printSlice(s)
s = append(s, 44)
printSlice(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment