golang close_chan
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang close_chan相关的知识,希望对你有一定的参考价值。
package memo
type entry struct {
res result
ready chan struct{}
}
func (e *entry) call(f Func, key string) {
e.res.value, e.res.err = f(key)
close(e.ready)
}
func (e *entry) deliver(response chan<- result) {
<-e.ready
response <- e.res
}
type result struct {
value interface{}
err error
}
type request struct {
key string
response chan<- result
}
type Memo struct {
requests chan request
}
func (memo *Memo) server(f Func) {
cache := make(map[string]*entry)
for req := range memo.requests {
e := cache[req.key]
if e == nil {
e = &entry{ready: make(chan struct{})}
cache[req.key] = e
go e.call(f, req.key)
}
go e.deliver(req.response)
}
}
type Func func(key string) (value interface{}, err error)
// factory func
func New(f Func) *Memo {
memo := &Memo{requests: make(chan request)}
go memo.server(f)
return memo
}
func (memo *Memo) Get(key string) (interface{}, error) {
response := make(chan result)
memo.requests <- request{key, response}
res := <-response
return res.value, res.err
}
func (memo *Memo) Close() {
close(memo.requests)
}
package memo
import (
"sync"
)
type entry struct {
res result
ready chan struct{}
}
type result struct {
value interface{}
err error
}
type Memo struct {
f Func
mu sync.Mutex
cache map[string]*entry
}
type Func func(key string) (value interface{}, err error)
// factory func
func New(f Func) *Memo {
return &Memo{f: f, cache: make(map[string]*entry)}
}
func (memo *Memo) Get(key string) (interface{}, error) {
memo.mu.Lock()
e := memo.cache[key]
if e == nil {
e = &entry{ready: make(chan struct{})}
memo.cache[key] = e
memo.mu.Unlock()
e.res.value, e.res.err = memo.f(key)
close(e.ready)
} else {
memo.mu.Unlock()
<-e.ready
}
return e.res.value, e.res.err
}
json [Golang] golang #golang #snippets中有用的片段
[ fmt ](https://golang.org/pkg/fmt/)
-------
Print any `type`(struct,maps,primitives) with the `key` name
```
fmt.Printf("\n [key] :%+v \n", [value])
```
Print Error
```
fmt.Errorf(" \nError: %s", err.Error())
```
[ log ](https://golang.org/pkg/log/)
-------------
Print any `type`(struct,maps,primitives) with the `key` name
```
log.Printf("\n [key] :%+v \n", [value])
```
[ http ](https://golang.org/pkg/log/)
-------------
http middleware
```
func [middlewareName](h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Do things before request
nextMiddleware.ServeHTTP(w, r)
// Do things after request
})
}
```
{
"http Middleware declaration": {
"prefix": "md",
"body": ["func $1(${2:nextMiddleware} http.Handler) http.Handler {\n\treturn http.HandlerFunc(func(${3:w} http.ResponseWriter, ${4:r} *http.Request) {\n\t $5\n\t nextMiddleware.ServeHTTP(${3:w}, ${4:r})\n\t $6\n\t})\n}"],
"description": "Snippet for http middleware declaration"
},
"log.Printf()": {
"prefix": "lf",
"body": [
"log.Printf(\"\\n## ${1:text} ##: %+v\\n\", ${2:value})"
],
"description": "log.Printf()"
},
"fmt.Printf()": {
"prefix": "ff",
"body": [
"fmt.Printf(\"\\n## ${1:text} ## :%+v \\n\", ${2:value})",
],
"description": "log.Printf()"
}
}
以上是关于golang close_chan的主要内容,如果未能解决你的问题,请参考以下文章
Golang 入门
Golang入门到项目实战 第一个golang应用
golang编译androidso无法加载
golang如何打印内存内容
Golang入门到项目实战 golang匿名函数
json [Golang] golang #golang #snippets中有用的片段