Use of golang's comma ok pattern
/* @Time : 2019-02-23 11:49 @Author : Tenlu @File : ok @Software: GoLand */ package main import "fmt" // Comma ok modefunc main() { useMap() useChan() userType() useType2() } //1. Detect whether the map contains the specified keyfunc useMap() { company := map[string]string{"polly": "tencent", "robin": "baidu", "jack": "alibaba", "tenlu": "itech8"} if _, ok := company["toms"]; !ok { ("key toms is not exists") } for ck, cv := range company { (ck + "->" + cv) } } // 2. Check whether chan is closedfunc useChan() { ch := make(chan int, 10) for i := 1; i <= 5; i++ { ch <- i } close(ch) // The chan assignment ends, the channel must be closed elem := <-ch (elem) // 1 // FIFO queue, the chan sent first must be received first for cc := range ch { (cc) } // ch <-8 // At this time, sending data to ch chan, an error will be reported because the channel has been closed. panic:send on closed channel elem2 := <-ch elem3 := <-ch ("elem2:%d\n", elem2) // 0, because the channel has been closed ("elem3:%d\n", elem3) // 0 if _, isClosed := <-ch; !isClosed { ("channel closed") } // Go does not have a while loop, here is similar to while(true) in other languages for { i, ok := <-ch if !ok { ("channel closed!") break } (i) } } // 3. Detect whether the interface type is implementedtype I interface { // There is an interface for method I Get() Int } type Int int // Int type implements I interfacefunc (i Int) Get() Int { return i } func userType() { var myint Int = 5 var inter I = myint // Assign variables to interface val, ok := inter.(Int) ("%v, %v\n", val, ok) // The output is: 5, true} func useType2() { // The null value of chan type is nil, and it needs to be used with make after declaration. var ch=make(chan interface{},10) ch<-10 ch<-"jack" ch<-map[string]interface{}{"name":"jack","age":11} close(ch) // If you do not close the channel, an error will be reported during traversal. ("%v\n",<- ch) for c:=range ch { ("for:%v\n",c) ("type:%T\n", c) if newValue,ok:=c.(map[string]interface{});ok{ ("type:%s\n",(c)) // Get variable type // map[string]interface {} if _,ok:=newValue["name"];ok{ for k,v:=range newValue { ("%v->%v,\n",k,v) } } } } ("%v\n",<- ch) // nil } func useType2() { // The null value of chan type is nil, and it needs to be used with make after declaration. var ch=make(chan interface{},10) ch<-10 ch<-"jack" ch<-map[string]interface{}{"name":"jack","age":11} close(ch) // If you do not close the channel, an error will be reported during traversal. ("%v\n",<- ch) for c:=range ch { ("for:%v\n",c) ("type:%T\n", c) if newValue,ok:=c.(map[string]interface{});ok{ ("type:%s\n",(c)) // Get variable type // map[string]interface {} if _,ok:=newValue["name"];ok{ for k,v:=range newValue { ("%v->%v,\n",k,v) } } } } ("%v\n",<- ch) // nil }
The comments in the code are very detailed. If you need it, you can study and practice it yourself.
The above is the detailed content of the golang comma ok mode integration demo. For more information about golang comma ok mode, please follow my other related articles!