1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-04-23 11:07:43 +02:00

61 lines
974 B
Go
Raw Normal View History

2020-12-26 15:17:20 +00:00
package main
import (
"fmt"
"os"
2020-12-26 15:17:20 +00:00
2021-10-12 12:55:53 +01:00
"go-micro.dev/v4/config"
2021-10-13 13:31:23 +01:00
"github.com/asim/go-micro/plugins/config/encoder/toml/v4"
2021-10-12 12:55:53 +01:00
"go-micro.dev/v4/config/source"
"go-micro.dev/v4/config/source/file"
2020-12-26 15:17:20 +00:00
)
func main() {
// new toml encoder
t := toml.NewEncoder()
// create a new config
c, err := config.NewConfig(
config.WithSource(
// create a new file source
file.NewSource(
// path of file
file.WithPath("./example.conf"),
// specify the toml encoder
source.WithEncoder(t),
),
),
)
if err != nil {
fmt.Println(err)
return
}
// load the config
if err := c.Load(); err != nil {
fmt.Println(err)
return
}
// set a value
c.Set("foo", "bar")
// now the hacks begin
vals := c.Map()
// encode
v, err := t.Encode(vals)
if err != nil {
fmt.Println(err)
return
}
// write the file
if err := os.WriteFile("./example.conf", v, 0644); err != nil {
2020-12-26 15:17:20 +00:00
fmt.Println(err)
return
}
fmt.Println("wrote update to example.conf")
}