Hier ist meine JSON-Datei:Abstellungs JSON zurückgibt leer struct
{
"database": {
"dialect": "mysql"
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
Hier ist mein 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:\n%v", string(fileBytes))
fmt.Printf("Conf: %v\n", Conf)
fmt.Printf("Content: \n %v \nType: %T", Conf.Database.Host, Conf)
}
Und hier ist die Ausgabe:
File content:
{
"database": {
"dialect": "mysql"
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
Conf: {{ }}
Content:
Type: config.ConfigType%
Das Paket main
importiert und nur die Config
Funktion wird ausgeführt. Ich habe mir viele ähnliche Fragen angeschaut und es scheint, als hätte ich fast den gleichen Code wie in den Antworten, aber ich kann meinen Code nicht zum Laufen bringen.