1

Here is my directory structure:

 my_project
      my_api
        main.go
      util_dir
        util.go 

I am passing some environment variables in the init function inside the main.go since I want them to be sourced whenever my service starts.
Below is the main.go code snippet:

import (
   "net/url"  
   "net"  
) 

func init() {
     ...

     data := url.Values{}
     data.Add("client_id", os.Getenv("CLIENTID"))
     data.Add("client_secret", os.Getenv("CLIENTSECRET"))
}

I want to do something like this inside util.go :

 import (
      "os"
      "github.com/my_project/my_api"
 ) 

 func validation() {

    data.data.Add("scope", "xyzid")
    data.data.Add("another_secret", "SECRET")

    client := &http.Client{}
    req, err := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.data.Encode()))

 }

I am getting an error that import github.com/my_project/my_api is a program, not an importable package.

I want to know what is a work around for this problem?

1
  • You can't import a main package. I'm not sure what you're trying to do, but you if main imports the util package, you would have a circular dependency even if this were possible. Commented Feb 9, 2017 at 15:30

3 Answers 3

1

You can make a separate, third package that has your init function, which can export the values read from the environment variables. Then, both your main package and utility directory can read from them. Just by importing the package the init function will be read, so you can be sure the values are initialized in any program that imports it.

Sign up to request clarification or add additional context in comments.

1 Comment

I will give this a try @Carl Mastrangelo. Thanks!
1

import github.com/my_project/my_api is a program, not an importable package

above error said clearly that you cant import a program because it is not a package.

From what I can understand is you wanted your data variable to be shared across the package.

data := url.Values{} // you call this in your main package.

you can do this by creating another package in your util or somewhere else :

package otherPackage

var Data url.Values{}

func InitData(){
   // begin your init data
   Data = url.Values{}
   Data.Add("client_id", os.Getenv("CLIENTID"))
   Data.Add("client_secret", os.Getenv("CLIENTSECRET"))

}

and call this in your main package like this :

    package main
    import (
     ....
     "github.com/yourProject/otherPackage"
     ....
    )

    func init(){
        ohterPakcage.InitData()
    }

now you have a global variable called Data and you can call it in another package like :

otherPackage.Data // do what you want with that variable.

hope this help you.

2 Comments

Gujarat Santana, That helped. Thanks!
@Sanket I'm glad I could help. please kindly accept my answer if this help you. :D
0
import (
   "net/url"  
   "net"  
) 

var Data = init()

func init() url.Values {
     ...

     data := url.Values{}
     data.Add("client_id", os.Getenv("CLIENTID"))
     data.Add("client_secret", os.Getenv("CLIENTSECRET"))
     return data
}

now in util.go you can use data like blow

import (
      "os"
      "github.com/my_project/my_api"
 ) 

 func validation() {

    my_api.Data.Add("scope", "xyzid")
    my_api.Data.Add("another_secret", "SECRET")

    client := &http.Client{}
    req, err := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.data.Encode()))

 }

Comments

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.