を返します。非整列化JSONは、ここに私のJSONファイルだ空のstruct
{
"database": {
"dialect": "mysql"
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
ここに私のコードです:
File content:
{
"database": {
"dialect": "mysql"
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
Conf: {{ }}
Content:
Type: config.ConfigType%
パッケージがmain
にインポートされます。
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)
}
そして、ここでは、出力されますConfig
関数だけが実行されます。私は似たような質問をたくさん見てきましたが、答えとまったく同じコードをほとんど持っているようですが、私のコードを動作させることはできません。