'How to initialize unexported structure from another package
I have an unexported Go struct in some package. And I need to initialize and pass it to some func from main package (for testing purposes). How can I initialize this unexported struct? Maybe there are some options with reflect?
Solution 1:[1]
If you really need to do this, the correct way is to vendor the code. I don't mean vendor in the sense of go mod vendor (although that might work), but just copying the code from wherever it is online, and moving it into your own package.
If the function in question is in your own package, then it doesn't matter if it's exported or not. This is not ideal solution, especially if the function is heavily ingrained in the original source, but might work for you. As others noted, without some reflect hacks, it's not really possible to do what you're asking in the original question. If someone didn't export a function, it's because they don't want you doing what you're trying to do.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | halfer |
