I have the code which parses json config:
import (
"encoding/json"
"os"
"fmt"
)
type Configuration struct {
Users []string
Groups []string
}
type AnotherConfiguration struct {
Names []string
}
file, _ := os.Open("conf.json")
decoder := json.NewDecoder(file)
configuration := Configuration{}
err := decoder.Decode(&configuration)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(configuration.Users)
As you can see, I have two different types Configuration and AnotherConfiguration.
I can't quite figure out how to create a generic function, which would return a config for any type (Configuration or AnotherConfiguration).
Something like this:
func make(typename) {
file, _ := os.Open("conf.json")
decoder := json.NewDecoder(file)
configuration := typename{}
err := decoder.Decode(&configuration)
if err != nil {
fmt.Println("error:", err)
}
return configuration
}