通过 GO 中的 Json 文件导入数据不符合预期 - fmt.Println 仅将神秘的输出返回给提示

问题描述 投票:0回答:1
package main

import (
    "flag"
    "fmt"

    "github.com/peterbourgon/ff"
)

var (
    compartmentOCID,
    bucketName,
    namespace *string
)

func main() {
    // Load config from JSON file
    compartmentOCID, bucketName, namespace := loadConfig()

    fmt.Println("Compartment OCID:", compartmentOCID)
    fmt.Println("Object Storage bucket name:", bucketName)
    fmt.Println("Object Storage namespace:", namespace)
}

// Load config
func loadConfig() (compartmentOCID *string, bucketName *string, namespace *string) {

    // Load creds from JSON into vars
    fs := flag.NewFlagSet("config", flag.ExitOnError)

    compartmentOCID = fs.String("compartment-ocid", "", "Compartment OCID")
    bucketName = fs.String("bucket", "", "Object Storage bucket name")
    namespace = fs.String("namespace", "", "Object Storage namespace")

    //  consumerKey = fs.String("consumerKey", "", "Twitter Consumer API key")
    //  consumerSecret = fs.String("consumerSecret", "", "Twitter Consumer API secret")

    //  accessToken = fs.String("accessToken", "", "Twitter Access Token")
    //  accessTokenSecret = fs.String("accessTokenSecret", "", "Twitter Access Token Secret")

    // senderEmail = fs.String("senderEmail", "", "Sender email")
    // recipientEmail = fs.String("recipientEmail", "", "Recipient email")

    //  ff.Parse(fs, nil, ff.WithConfigFile("config.json"), ff.WithConfigFileParser(ff.JSONParser))

    ff.Parse(fs, []string{},
        ff.WithConfigFile("config.json"),
        ff.WithConfigFileParser(ff.JSONParser))

    return compartmentOCID, bucketName, namespace
}

我是 GO 新手,玩了一下。不确定我可能会错过什么,但我只收到以下输出:

隔室 OCID:0x1400008e040
对象存储桶名称:0x1400008e050
对象存储命名空间:0x1400008e060

这就是 json 中的内容:

“bucket”:“推特到电子邮件”,
“舱室-ocid”:“ocid1.tenancy.oc1..aaaaaaaa4gmjp4otjth423bk5aqnynhbhvpdok3a5ka4dtmcgytsciqsw3xq”,
“命名空间”:“frvhi6pjrbat”,

json go variables output
1个回答
0
投票

函数

loadConfig
返回指向字符串的指针。指针被打印为内存地址。

要获得预期结果,请将函数更改为返回字符串。

func loadConfig() (compartmentOCID string, bucketName string, namespace string) {
   ... other code as is
   return *compartmentOCID, *bucketName, *namespace
}
© www.soinside.com 2019 - 2024. All rights reserved.