But the interesting point is how code is using this struct. There are good reasons to export or to not export members. As long as we don’t know how the struct is to be used, we can neither advise on exporting nor can we help solving the problem.
I just assumed it was something JSON related perhaps, because he did not get any errors.
@Derek_Robson,
If you are infact using unexported members of a struct in a different package, that should throw an error because it cannot identify such a struct member.
My use case is to import a config via yaml file and have it available across all functions.
I know I can make it work if I use a Capital letter at start of struct member name.
My real point is that the example below is “broken” yet give no errors or warnings.
Should go give me a warning that I am doing stupid things?
I am sure there are many ways to make the code work, but should it give me an error?
This is less about how i should write my code, but tips are welcome, its more that this complies fine and won’t work.
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v2"
)
var data = `
a: Easy!
`
type Test struct {
aA string
}
var test Test
func main() {
err := yaml.Unmarshal([]byte(data), &test)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("test.aA =: %v\n", test.aA)
}
The json package only accesses the exported fields of struct types (those that begin with an uppercase letter). Therefore only the the exported fields of a struct will be present in the JSON output.
And:
Unmarshal will decode only the fields that it can find in the destination type. […] It also means that any unexported fields in the destination struct will be unaffected by Unmarshal .
Since reflection is used from outside your module, only exported members are visible to the library code that looks for members to populate. It has no way to even know that there are unexported members that might match JSON/YAML object properties.
From the POV of the library code populating the members, this is a good thing. Unexported members are for internal use of the strcut and not to be messed with by code external to the module.