go equal

https://www.jianshu.com/p/40c4ba729eac

 

 

package main

import (
"context"
"fmt"
"time"
)

func emain() {
d := time.Now().Add(1 * time.Second)
ctx, cancel := context.WithDeadline(context.Background(), d)


// Even though ctx will be expired, it is good practice to call its
// cancelation function in any case. Failure to do so may keep the
// context and its parent alive longer than necessary.
defer cancel()

select {
case <-time.After(2 * time.Second):
fmt.Println("oversleep")
case <-ctx.Done():
fmt.Println(ctx.Err())
}
}

var key = "key"

func cmain() {
ctx, cancel := context.WithCancel(context.Background())

valueCtx := context.WithValue(ctx, key,"add value")

go watch(valueCtx)
time.Sleep(10 * time.Second)
cancel()

//time.Sleep(2 * time.Second)
}

func watch(ctx context.Context) {
for {
select {
case <-ctx.Done():
//get value
fmt.Println(ctx.Value(key), "is cancel")

return
default:
//get value
fmt.Println(ctx.Value(key), "int goroutine")

time.Sleep(1 * time.Second)
}
}
}

func zmain() {
type A struct {
a int
b string
}

aa := &A { a : 1, b : "test1" }
bb := &A { a : 1, b : "test1" }
cc := aa
fmt.Println(aa == bb)
fmt.Println(aa == cc)
}

func main() {
type A struct {
a int
b string
}

var aa interface{} = A { a: 1, b: "test" }
var bb interface{} = A { a: 1, b: "test" }
var cc interface{} = A { a: 2, b: "test" }

fmt.Println(aa == bb) // true
fmt.Println(aa == cc) // false

var dd interface{} = &A { a: 1, b: "test" }
var ee interface{} = &A { a: 1, b: "test" }
fmt.Println(dd == dd) //true
fmt.Println(dd == ee) // false
}




 

posted @ 2020-06-11 13:52  zJanly  阅读(170)  评论(0)    收藏  举报