alt
codecamp
Golang Essentials: Slices and Maps
Previous
Next
☰
About
Profile
Account
Login
Sign Up
Logout
Run
Test
Mark Complete
## Slices and Maps in Go Go provides two powerful built-in types for managing collections of data: slices and maps. ### Slices A slice is a dynamically-sized, flexible view into the elements of an array. #### Creating Slices You can create a slice using the built-in `make` function: ```go s := make([]int, 0) // Creates an empty slice of ints ``` Or by slicing an array: ```go arr := [5]int{1, 2, 3, 4, 5} s := arr[1:4] // Creates a slice including elements 2, 3, and 4 ``` ### Maps A map is a collection of key-value pairs. The keys are unique within a map while the values may not be. #### Creating Maps Maps can be created using the `make` function: ```go m := make(map[string]int) ``` Or using map literals: ```go m := map[string]int{"foo": 1, "bar": 2} ``` ### Conclusion Understanding slices and maps is essential in Go as they are widely used for data storage and manipulation. They offer powerful and flexible ways to handle collections of data.