Sanket
Sanket

Reputation: 351

How to import a variable from an init function (inside main.go) to a different file in go?

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?

Upvotes: 1

Views: 706

Answers (3)

Jeyem
Jeyem

Reputation: 310

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()))

 }

Upvotes: 0

Gujarat Santana
Gujarat Santana

Reputation: 10564

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.

Upvotes: 1

Carl Mastrangelo
Carl Mastrangelo

Reputation: 6638

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.

Upvotes: 1

Related Questions