-1

我正在尝试创建一个包含配置信息的结构,但我无法使其工作:

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

type Database struct {
    Server       string `json:"server"`
    ServerPort   int    `json:"server_port"`
    Username     string `json:"username"`
    Password     string `json:"password"`
    DatabaseName string `json:"database_name"`
}

type CachedFile struct {
    Year         int    `json:"year"`
    FilePath     string `json:"file_path"`
    CreationDate string `json:"creation_date"`
    Rows         int    `json:"rows"`
}

type Config struct {
    database    Database
    fileStore   string
    cachedFiles map[int]CachedFile
}

func main() {

    config := Config{

        database: Database{
            Server:       "dbserver.io",
            ServerPort:   3306,
            Username:     "TheUsername",
            Password:     "ThePassword",
            DatabaseName: "TheDatabase",
        },

        fileStore: "./feather_files",

        cachedFiles: {
            2000: CachedFile{
                FilePath:     "./feather_files/test_2020.txt",
                CreationDate: "01-01-2020",
                Rows:         1020,
                Year:         2000,
            },
        },
    }

    //MarshalIndent
    theJSON, err := json.MarshalIndent(config, "", "  ")
    if err != nil {
        log.Fatalf(err.Error())
    }
    fmt.Printf("MarshalIndent function output %s\n", string(theJSON))
}

当我使用 Go Jupyter Notebook 时,此代码有效。当我为“真实”运行它时,我在 vscode 和编译器中得到一个“复合文字中的缺失类型” ,与这部分相关:

cachedFiles: {2000 : CachedFile{
                FilePath:     "./feather_files/test_2020.txt",
                CreationDate: "01-01-2020",
                Rows:         1020,
                Year:         2000,
    },
4

0 回答 0