Golang配置文件应加载自己的程序包

This question already has an answer here:

I have two projects those are 1. Web Project 2. Broker Project

services
    -web/util.go
    -main.go
    -cofig.json


broker
    -consumer/redis.go
    -consumer/mongo.go
    -main.go
    -<cofig.go>   //I do not want to make a copy here

I have developed an utility program for my web project which uses config.json file. In that utility program I use the code like

file, _ := os.Open("./config.json")

That is working fine. When I included this project into Broker project like

package "services/web"

And I tried to utilize utility code in it but it tries to find 'config.json' from Broker project instead of "services web project". Due to this reason I need to copy the same file inside the Broker project.

Please help me to keep single file inside 'services/web'

Thanks is advance.

Regards, Dinesh

</div>

You need to approach the problem differently. You are compiling your code to a single binary file, so by the time your program is actually running, it has lost all of its package structure.

The simplest solution is to modify services/web to accept a web.Config struct instead of loading from a file. Then the consumer (the broker package in this case) is responsible for providing the config. It can use a file if it wants to.