请教一个包导入问题,百思不得解!!!

weixin_38069603 2019-09-20 10:10:55
项目结构如下:
```
decoder
├── config.json
└── decoder.go
```
config.json 文件内容
```
{
"ServerInfo": {
"Host": "127.0.0.1:8888"
},
"RedisInfo": {
"Host": "127.0.0.1:6379",
    "MaxIdle": 16,
     "MaxActive": 0,
     "IdleTimeout": 300
}
}
```
decoder.go 文件内容
```
package main

import (
    "encoding/json"
    "fmt"
    "os"
    "time"
)

type Config struct{}

type ConfigurationType struct {
    ServerInfo ServerInfoType
    RedisInfo RedisInfoType
}

type ServerInfoType struct {
    Host string
}

type RedisInfoType struct {
    Host string
    MaxIdle int
    MaxActive int
    IdleTimeout time.Duration
}

var Configuration = ConfigurationType{}

func (this Config) InitConfig() {
    file, _ := os.Open("config.json")
    defer file.Close()
    decoder := json.NewDecoder(file)
    Configuration = ConfigurationType{}
    err := decoder.Decode(&Configuration)
    if err != nil {
        fmt.Println("Error: ", err)
    }
    fmt.Printf("Configuration: %v\n", Configuration)
}

func main() {
    conf := Config{}
    conf.InitConfig()
}
```
这种情况下我运行:`go run decoder.go` , 输出结果如下:
```
Configuration: {{127.0.0.1:8888} {127.0.0.1:6379 16 0 300ns}}
```

但是我把这个 main 包改为 decoder 包,项目结构调整如下
```
.
├── decoder
│ ├── config.json
│ └── decoder.go
└── main.go
```
config.json 文件内容
```
{
"ServerInfo": {
"Host": "127.0.0.1:8888"
},
"RedisInfo": {
"Host": "127.0.0.1:6379",
    "MaxIdle": 16,
     "MaxActive": 0,
     "IdleTimeout": 300
}
}
```
decoder.go 文件内容
```
package decoder

import (
    "encoding/json"
    "fmt"
    "os"
    "time"
)

type Config struct{}

type ConfigurationType struct {
    ServerInfo ServerInfoType
    RedisInfo RedisInfoType
}

type ServerInfoType struct {
    Host string
}

type RedisInfoType struct {
    Host string
    MaxIdle int
    MaxActive int
    IdleTimeout time.Duration
}

var Configuration = ConfigurationType{}

func (this Config) InitConfig() {
    file, _ := os.Open("config.json")
    defer file.Close()
    decoder := json.NewDecoder(file)
    Configuration = ConfigurationType{}
    err := decoder.Decode(&Configuration)
    if err != nil {
        fmt.Println("Error: ", err)
    }
    fmt.Printf("Configuration: %v\n", Configuration)
}
```
main.go 文件内容
```
package main

import "initTest/decoder"

func main() {
    t := decoder.Config{}
    t.InitConfig()
}
```
测试运行 `go run main.go` 报错如下:
```
Error: invalid argument
Configuration: {{} { 0 0 0s}}
```
请问是否有人知道是什么原因?
...全文
39 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38080213 2019-09-20
  • 打赏
  • 举报
回复
为什么非单文件的,大家还是喜欢 go run 单个文件呢~ 能 go build 先编译吗
weixin_38093364 2019-09-20
  • 打赏
  • 举报
回复
os.Open读取的文件是不是没有读取到?把配置文件放到main的同一层路径下试试呢.
weixin_38101916 2019-09-20
  • 打赏
  • 举报
回复
读取文件的名称 改成decoder/config.json
weixin_38104241 2019-09-20
  • 打赏
  • 举报
回复
是文件路径问题。目前用的语言中,读取文件的相关函数,在文件路径错误的时候直接会抛异常,Golang 这种包底层直接返回 err 的做法还是又点儿不习惯,导致排查文件,压根没有想到是文件路径错误。 ``` ..... func (this Config) InitConfig() { file, err := os.Open("./config.json") defer file.Close() if err != nil { fmt.Printf("Open file error: %v\n", err) } decoder := json.NewDecoder(file) Configuration = ConfigurationType{} err = decoder.Decode(&Configuration) if err != nil { fmt.Println("Error: ", err) } fmt.Printf("Configuration: %v\n", Configuration) } ``` 打印了相关 Error,发现了是文件不存在。

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧