View deep_copy_maps.go
package main | |
import ( | |
"encoding/json" | |
"errors" | |
"fmt" | |
) | |
func deepCopyMap(src map[string]int, dst map[string]int) error { | |
if src == nil { |
View nested_maps.go
package main | |
import "fmt" | |
func main() { | |
// shoppingList is a map that has a map inside it | |
shoppingList := make(map[string]map[string]int) | |
// veggies key points to veggiesMap | |
veggiesMap := map[string]int{"onion": 2, "orka": 3} |
View maps_as_references.go
package main | |
import "fmt" | |
// Function tp update maps | |
func updateNumbers(m map[string]int) { | |
m["three"] = 3 | |
m["four"] = 4 | |
} |
View working_with_maps.go
package main | |
import "fmt" | |
func main() { | |
var mscores = make(map[string]int) | |
mscores["Chetan"] = 90 | |
mscores["John"] = 75 | |
mscores["Alice"] = 30 |
View declare_maps.go
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
var mscores = make(map[string]int) | |
mscores["Chetan"] = 90 | |
// Returns mscrores: map[Chetan:90] |
View slices_functions.go
package main | |
import ( | |
"fmt" | |
) | |
/* Function that doubles every element in the slice */ | |
func workonslice(slice []int) { | |
for i := range slice { | |
slice[i] *= 2 |
View slices_append_newarray.go
package main | |
import ( | |
"fmt" | |
"reflect" | |
"unsafe" | |
) | |
func main() { |
View slices_append.go
package main | |
import "fmt" | |
func main() { | |
var names []string /* Zero value of a slice is nil */ | |
fmt.Println(names, len(names), cap(names)) /* Returns [] 0 0 */ | |
names = append(names, "John", "Bill", "Steve") | |
fmt.Println(names, len(names), cap(names)) /* Returns [John Bill Steve] 3 3 */ |
View slices_declare.go
package main | |
import "fmt" | |
func main() { | |
slices := []string{"AB", "CD", "EF"} | |
fmt.Println(slices) | |
array := [5]int{76, 77, 78, 79, 80} |
View array_compare.go
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
/* Shorthand declaration */ | |
scores := [4]int{80, 85, 45, 55} |
NewerOlder