Skip to content

Instantly share code, notes, and snippets.

@Thedrogon
Last active April 27, 2025 08:09
Show Gist options
  • Save Thedrogon/a4a0407b065ee8ac283ff4fa4d4878f9 to your computer and use it in GitHub Desktop.
Save Thedrogon/a4a0407b065ee8ac283ff4fa4d4878f9 to your computer and use it in GitHub Desktop.
A Gist that shows how Arrays , maps, slices are done in Golang ...
func main(){
//Arrays in Golang
my_array := [5]int{1,2,3,4,5} // just like othesrs , arrays in go are 0 indexed.
//multidimensional arrys
matrix := [2][3]int{
{1,2,3},
{4,5,6},
}
//In case the size of array is not known we can use Slices.
//Slices can be a whole dynamic array as well it can a sits name suggests a part of a well defined array
allnumber := my_array[:] //in this case allnumber is slice copy of the my_array array , but in this case we can append more values to allnumber as its a slice .
//lets say we wanna append a data in the slice
allnumber := append(allnumber , 6} //works exactly like arraylists in java.
fruits := []string{}
fruits = append(fruits, "apple")
// appending 2 slices together
morefruits := []string{"mango","banana"}
morefruits = append(fruits ,morefruits...)
//Maps in go are key - value pairs
name_marks := map[string]int{
"sayan":46,
"mili":50,
}
//checking if a key is present in the map or not
value , exists := name_marks["raj"]
if exists {
fmt.Printf(" Raj got %d marks ",value)
}else{
fmt.Print("Raj isnt on the map")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment