Several Fun Things to Know in Golang

Credits to Greyson Joralemon (https://unsplash.com/photos/9IBqihqhuHc)
var i interface{}
var pint *int
i = pint
fmt.Println(pint, pint == nil, i, i == nil)
<nil> true <nil> false
func sliceAndMap() {
mp := map[int]bool{
1: true,
2: true,
}
slc := []int{1,2}

modify(mp, slc)
fmt.Printf("%+v || %+v\n", mp, slc)}func modify(mp map[int]bool, slc []int) {
mp[2] = false
slc[0] = 10
}
map[1:true 2:false]  ||  [10 2]
func main() {   goParent()
}
func goParent() { fmt.Println("Parent Start") go func() { fmt.Println("Child is done") }() // time.Sleep(5 * time.Millisecond) fmt.Println("Parent is done")}
Parent Start
Parent is done
ctx := context.WithValue(ctx, key, value)
should not use basic type string as key in context.WithValue
type CKRequestID intconst (  RequestIDContextKey CKRequestID = iota + 1)
func ContextKeyCollision() { ctx := context.WithValue(context.Background(), RequestIDContextKey, "bb") ctx = context.WithValue(ctx, 1, "cc") ctx = context.WithValue(ctx, 1, "ff") usingKey := ctx.Value(RequestIDContextKey) usingInt := ctx.Value(1) fmt.Println(usingKey, usingInt)}
bb ff

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store