Parsing Json File With Golang
Example to read json config file in golang
package config
import (
"encoding/json"
"os"
)
type Config struct {
Database string `json:"database,omitempty"`
ConnectionString string `json:"connectionString,omitempty"`
}
func ReadConfig(filepath string) (*Config, error) {
data, err := os.ReadFile(filepath)
if err != nil {
return nil, err
}
config := new(Config)
if err := json.Unmarshal(data, &config); err != nil {
return nil, err
}
return config, nil
}