SingleHash(done, in[1:])
как горутину, чтобы не блокировать main и перейти к ожиданию ответа из канала done. И в данном случае go func(done chan interface{})
нужно будет выполнить синхронно, а не горутиной. The returned DB is safe for concurrent use by multiple goroutines and maintains its own pool of idle connections. Thus, the Open function should be called just once. It is rarely necessary to close a DB.
package main
import (
"encoding/json"
"fmt"
)
type TemplateCategory struct {
ID int `json:"id"`
Name string `json:"name"`
}
type Template struct {
Name string `json:"name"`
CategoryInfoRaw *json.RawMessage `json:"category_info"`
CategoryInfo *TemplateCategory
}
func (t *Template) UnmarshalJSON(data []byte) error {
type template Template
if err := json.Unmarshal(data, (*template)(t)); err != nil {
return err
}
if t.CategoryInfoRaw == nil || string(*t.CategoryInfoRaw) == "[]" {
t.CategoryInfo = nil
} else {
if err := json.Unmarshal(*t.CategoryInfoRaw, &t.CategoryInfo); err != nil {
return err
}
}
return nil
}
func main() {
res := &Template{}
str1 := `{
"name": "Мой шаблон",
"category_info": {
"id": 109,
"name": "Тест"
}
}`
str2 := `{
"name": "Мой шаблон",
"category_info": []
}`
_ = json.Unmarshal([]byte(str1), res)
fmt.Printf("%+v\n", res)
fmt.Printf("%+v\n", res.CategoryInfo)
_ = json.Unmarshal([]byte(str2), res)
fmt.Printf("%+v", res)
}
case <-ctx.Done():
return nil, ctx.Err()
}
case <-ctx.Done():
err = fmt.Errorf("lost CorrelationId=%s %s", corrID, err.Error())
c.logger.Error(err)
return nil, ctx.Err()
}
strings.ReplaceAll("+999 99 999 99 99", " ", "")
It may be great to have built-in HTTP caching of some kind, but it is way better to use a Content-Delivery Network or a caching proxy server for this, as you will have to do this sooner or later in the production environment.
func fff() {
count := 0
for {
print(count)
count += 1
}
}