如何将 init 函数(在 main.go 内部)中的变量导入到 Go 中的另一个文件中?

huangapple go评论80阅读模式
英文:

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

问题

这是我的目录结构:

 my_project
      my_api
        main.go
      util_dir
        util.go 

我在main.goinit函数中传递了一些环境变量,因为我希望它们在我的服务启动时被加载。以下是main.go的代码片段:

import (
   "net/url"  
   "net"  
) 

func init() {
     ...

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

我想在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()))

 }

我得到了一个错误,提示import github.com/my_project/my_api is a program, not an importable package

我想知道如何解决这个问题?

英文:

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

得分: 1

你可以创建一个单独的第三方包,其中包含你的init函数,该函数可以导出从环境变量中读取的值。然后,你的主包和实用程序目录都可以从中读取这些值。只需导入该包,init函数就会被读取,这样你可以确保在导入它的任何程序中都初始化了这些值。

英文:

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.

答案2

得分: 1

> import github.com/my_project/my_api 是一个程序,而不是可导入的包。

上述错误明确表示您不能导入一个程序,因为它不是一个包。

从我理解的情况来看,您希望在整个包中共享data变量。

您可以通过在util或其他地方创建另一个包来实现这一点:

package otherPackage

var Data url.Values{}

func InitData(){
   // 初始化数据
   Data = url.Values{}
   Data.Add("client_id", os.Getenv("CLIENTID"))
   Data.Add("client_secret", os.Getenv("CLIENTSECRET"))
}

然后在您的主包中这样调用:

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

func init(){
    otherPackage.InitData()
}

现在您有一个名为Data的全局变量,您可以在另一个包中调用它:

otherPackage.Data // 对该变量进行操作

希望这对您有所帮助。

英文:

> 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.

答案3

得分: 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

}

现在在util.go中,你可以像下面这样使用data

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

}

英文:
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()))

 }

huangapple
  • 本文由 发表于 2017年2月9日 23:22:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/42140532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定