Skip to content

Instantly share code, notes, and snippets.

Avatar

Chetan Giridhar cjgiridhar

View GitHub Profile
@cjgiridhar
cjgiridhar / deep_copy_maps.go
Created Feb 20, 2019
Deep Copy Maps in Golang
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 {
@cjgiridhar
cjgiridhar / nested_maps.go
Created Feb 18, 2019
Nested Maps in Golang
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}
@cjgiridhar
cjgiridhar / maps_as_references.go
Created Feb 18, 2019
Golang maps are reference types
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
}
@cjgiridhar
cjgiridhar / working_with_maps.go
Created Feb 17, 2019
Working with maps in Golang
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
@cjgiridhar
cjgiridhar / declare_maps.go
Last active Feb 17, 2019
Declare maps in Golang
View declare_maps.go
package main
import (
"fmt"
)
func main() {
var mscores = make(map[string]int)
mscores["Chetan"] = 90
// Returns mscrores: map[Chetan:90]
@cjgiridhar
cjgiridhar / slices_functions.go
Created Jan 27, 2019
Passing slices to functions
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
@cjgiridhar
cjgiridhar / slices_append_newarray.go
Created Jan 27, 2019
Append in slices creates a new underlying array
View slices_append_newarray.go
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
@cjgiridhar
cjgiridhar / slices_append.go
Last active Jan 27, 2019
Appending elements to slices in Golang
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 */
@cjgiridhar
cjgiridhar / slices_declare.go
Created Jan 26, 2019
Declaring slices in Golang
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}
@cjgiridhar
cjgiridhar / array_compare.go
Created Jan 26, 2019
Comparing arrays in Golang
View array_compare.go
package main
import (
"fmt"
)
func main() {
/* Shorthand declaration */
scores := [4]int{80, 85, 45, 55}
You can’t perform that action at this time.