英文:
Wrong struct returned from separate package function
问题
我在Ubuntu 18.04上使用Go开发应用程序。
我有一个config包。想法是在其他包中使用这个包来获取配置。config包有一个名为_Config
的结构体变量。函数GetConfig
的想法是返回_Config
,当_Config
没有填充时,函数将首先从json文件中填充_Config
。
config包的代码如下:
package config
import (
"fmt"
"github.com/tkanos/gonfig"
)
type Config struct {
DB struct{
DBHost string
DBPort uint
DBUser string
DBPassword string
DBName string
}
}
var _Config Config
func GetConfig() Config {
if (Config{}) == _Config {
_Config := Config{}
err := gonfig.GetConf("/home/mike/go/src/project/config.json", &_Config)
if err != nil{
panic("Error when parsing config")
}
fmt.Println(_Config.DB)
}
return _Config
}
Println
的结果是:
{127.0.0.1 3306 user Password project}
主包的代码如下:
package main
import (
"fmt"
"project/config"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "project/config"
"syscall"
"unsafe"
)
func main() {
_config := config.GetConfig()
fmt.Println(_config)
}
Println
的结果是:
{{ 0 }}
json文件的内容如下:
{
"DB": {
"DBHost": "127.0.0.1",
"DBPort": 3306,
"DBUser": "user",
"DBPassword": "Password",
"DBName": "project"
}
}
那么为什么会这样呢?为什么我在主包中无法获取正确的配置?我认为问题在于我对Go在这种情况下的工作方式有错误的想象。
请为我澄清这一点,这种配置方式是否可行?如果不可行,有什么好的模式?我的想法是有一个用于配置的访问和负责的包,我可以在任何地方使用它。
英文:
I develop application on my Ubuntu 18.04 with go.
I have config package. The idea is to use this package in another packages to get configurations. Package config has struct variable _Config
. The idea that function GetConfig
will return _Config
, and when _Config
not filled the function will firstly fill _Config
from json file.
The config package looks like this:
package config
import (
"fmt"
"github.com/tkanos/gonfig"
)
type Config struct {
DB struct{
DBHost string
DBPort uint
DBUser string
DBPassword string
DBName string
}
}
var _Config Config
func GetConfig() Config {
if (Config{}) == _Config {
_Config := Config{}
err := gonfig.GetConf("/home/mike/go/src/project/config.json", &_Config)
if err != nil{
panic("Error when parsing config")
}
fmt.Println(_Config.DB)
}
return _Config
}
And the result of Println
is:
{127.0.0.1 3306 user Password project}
The code of main package looks like:
package main
import (
"fmt"
"project/config"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "project/config"
"syscall"
"unsafe"
)
func main() {
_config := config.GetConfig()
fmt.Println(_config)
}
And the result of Println
is:
{{ 0 }}
The json file looks like:
{
"DB": {
"DBHost": "127.0.0.1",
"DBPort": 3306,
"DBUser": "user",
"DBPassword": "Password",
"DBName": "project"
}
}
So why is that? Why I can't get correct config in main package? The problem I think that I have wrong imagination about how Go works in such situation.
Please clarify this moment for me, and is it good approach for config in application ? If not, what is good pattern? The idea is to have one access and one responsible package for config which I can use everywhere
答案1
得分: 3
你在GetConfig()
函数中有一行代码 - _Config := Config{}
。
在这里,你在局部作用域中声明了一个新变量_Config
,并遮蔽了全局变量。删除这行代码然后再试一次。由于遮蔽的原因,全局变量没有被json中的配置填充。
英文:
You have a line in GetConfig()
function -
_Config := Config{}
.
Here, you are declaring a new variable _Config
in local scope and shadowing the global variable. Remove this line and try again. Due to shadowing, global variable is not populated with config from json.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论