英文:
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.go
的init
函数中传递了一些环境变量,因为我希望它们在我的服务启动时被加载。以下是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()))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论