0

config.json

{
    "admins": [
        "AdminA",
        "AdminB"
    ],
    "apikey": "apikey"
}

main.go

package main

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

type MainConfiguration struct {
    admins []string `json:"admins"`
    apikey string `json:"apikey"`
}

func ParseConf() *MainConfiguration {
    f, _ := os.Open("config.json")
    defer f.Close()
    d := json.NewDecoder(f)
    m := &MainConfiguration{}
    d.Decode(m)
    return m
}

func main() {
    conf := ParseConf()
    fmt.Printf("%s", conf)
}

After looking for everything I could to fix this all I get is:

&{[] }

Whats wrong here, and why is this empty? Its basic and I've basically just copy/pasted from elsewhere and while this may be common, I'm not finding answers to this specific question.

1 Answer 1

3

You need to make fields in MainConfiguration public (note first capital letter):

type MainConfiguration struct {
    Admins []string `json:"admins"`
    Apikey string `json:"apikey"`
}
Sign up to request clarification or add additional context in comments.

1 Comment

I knew it would be something stupid I'm overlooking due to not working with Go 24/7.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.