all repos — disgord @ 2d217f4b2580d9476ffdac02449fe8a4b6da212e

A simple Discord bot in Go.

src/myconfig/config.go (view raw)

 1package myconfig
 2
 3import (
 4	"encoding/json"
 5	"os"
 6)
 7
 8type Config[T any] struct {
 9	filename string
10	Values   T
11}
12
13func New[T any](filename string) (c *Config[T], err error) {
14	fileContent, err := os.ReadFile(filename)
15	if err != nil {
16		return
17	}
18
19	c = &Config[T]{
20		filename: filename,
21	}
22
23	err = json.Unmarshal(fileContent, &c.Values)
24	return
25}
26
27func (c *Config[T]) Save() error {
28	fileContent, err := json.MarshalIndent(c.Values, "", "    ")
29	if err != nil {
30		return err
31	}
32
33	return os.WriteFile(c.filename, fileContent, 0644)
34}