Here's my JSON file:
{
"database": {
"dialect": "mysql"
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
Here's my code:
package config
import (
"fmt"
"encoding/json"
"io/ioutil"
"log"
"os"
)
type ConfigType struct {
Database DatabaseType `json:"database"`
}
type DatabaseType struct {
Dialect string `json:"dialect"`
Host string `json:"host"`
User string `json:"user"`
Pass string `json:"pass"`
Name string `json:"name"`
}
func Config() {
file, err := os.Open("./config/config.json")
if err != nil {
log.Fatal(err)
}
defer file.Close()
fileBytes, _ := ioutil.ReadAll(file)
var Conf ConfigType
json.Unmarshal(fileBytes, &Conf)
fmt.Printf("File content:
%v", string(fileBytes))
fmt.Printf("Conf: %v
", Conf)
fmt.Printf("Content:
%v
Type: %T", Conf.Database.Host, Conf)
}
And here is the output:
File content:
{
"database": {
"dialect": "mysql"
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
Conf: {{ }}
Content:
Type: config.ConfigType%
The package is imported to main
and just the Config
function is executed. I've looked at a lot of similar questions and it seems like I almost have the exact same code as in the answers but I can't get my code to work.
Errors are not graciously returned to you to omit, unless you wanna get clueless about why your app doesn't work. Don't omit errors! ioutil.ReadAll()
returns an error. json.Unmarshal()
returns an error. Do check those!
Should you added error check, json.Unmarshal()
returns:
panic: invalid character '"' after object key:value pair
Try this on the Go Playground.
Your input JSON is invalid. You have a missing comma in the "dialect"
line. Adding the missing comma (try it on the Go Playground):
Conf: {{mysql localhost root sws}}
Content:
localhost
Type: main.ConfigType
Don't neglect the errors. Always keep track of errors if function is returning one. It helps you find out if somethings goes wrong.
> In your case json file is invalid you missed "," after "dialect": "mysql" .
> valid json file should be (config.json )
{
"database": {
"dialect": "mysql",
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
>
>
> Modified code
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
)
type ConfigType struct {
Database DatabaseType `json:"database"`
}
type DatabaseType struct {
Dialect string `json:"dialect"`
Host string `json:"host"`
User string `json:"user"`
Pass string `json:"pass"`
Name string `json:"name"`
}
func main() {
file, err := os.Open("./config.json")
if err != nil {
log.Fatal(err)
}
defer file.Close()
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println("error reading file", err)
return
}
var Conf ConfigType
err = json.Unmarshal(fileBytes, &Conf)
if err != nil {
fmt.Println("unmarshalling error ", err)
return
}
fmt.Printf("File content:
%v
", string(fileBytes))
fmt.Printf("Conf: %v
", Conf)
fmt.Printf("Content:
%v
Type: %T", Conf.Database.Host, Conf)
}
> Output
File content:
{
"database": {
"dialect": "mysql",
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
Conf: {{mysql localhost root sws}}
Content:
localhost
Type: main.ConfigType