Last active
January 11, 2020 12:11
-
-
Save jreisinger/1d283d111a8f82e6529f34e8e934657c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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