가 여기 내 JSON 파일의 빈 구조체를 반환 패키지가 main
로 가져비 정렬 화 JSON은
File content:
{
"database": {
"dialect": "mysql"
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
Conf: {{ }}
Content:
Type: config.ConfigType%
: 여기
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
함수 만 실행됩니다. 비슷한 질문을 많이했는데 답안과 거의 똑같은 코드가있는 것 같지만 작동하도록 코드를 가져올 수는 없습니다.