0

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

dinu0101
  • 461
  • 2
  • 5
  • 18

1 Answers1

0

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.

chowey
  • 9,138
  • 6
  • 54
  • 84
  • @chawey I understood what you said. Now my concern is that when we build/install application for production then we need to change the path every time between window & Linux. Please guide me how to configure path to that work on both envionments – dinu0101 Dec 29 '17 at 13:37
  • Actually a good reference is the linked question: https://stackoverflow.com/questions/31059023/how-to-reference-a-relative-file-from-code-and-tests. – chowey Dec 31 '17 at 03:21
  • @chawey Thanks. I understand the Golang. How Golang works. – dinu0101 Dec 31 '17 at 16:41