Go
- 55 ответов
- 0 вопросов
39
Вклад в тег
Possible resource leak, 'defer' is called in the 'for' loop.
A "defer" statement invokes a function whose execution is deferred to the moment the surrounding function returns, either because the surrounding function executed a return statement , reached the end of its function body , or because the corresponding goroutine is panicking .
func (app App) Shutdown() {
for _, shutdown := range app.onShutdown {
if err := shutdown(); err != nil {
log.Println(err)
}
}
}
...
defer app.Shutdown()
Весь день голову ломаю.
package config
import (
"encoding/json"
"fmt"
"os"
)
type Cfg struct {
// Fields
}
var (
cfg *Cfg
)
func Config() *Cfg {
return cfg
}
func LoadConfig() error {
f, err := os.Open("./config/config.json")
if err != nil {
return fmt.Errorf("can't open config file: %w", err)
}
defer f.Close()
dec := json.NewDecoder(f)
var c *Cfg
if err := dec.Decode(c); err != nil {
return fmt.Errorf("can't decode config file: %w", err)
}
if err := validate(c); err != nil {
return fmt.Errorf("can't validate config file: %w", err)
}
cfg = c
return nil
}
func validate(cfg *Cfg) error {
// Logic
return nil
}