传递结构体

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

Passing around structs

问题

我是你的中文翻译助手,以下是翻译好的内容:

我刚开始学习Go语言,之前是使用Ruby。我正在尝试理解在没有类的情况下的代码结构,并且可能犯了想要用“Ruby方式”来编写Go代码的错误。

我正在尝试重构我的代码,使其更模块化/可读性更好,所以我将配置文件的加载移到了自己的包中。这样做是个好主意吗?

package configuration

import "github.com/BurntSushi/toml"

type Config struct {
    Temperatures []struct {
        Degrees int
        Units   string
    }
}

func Load() Config {
    var cnf Config
    _, err := toml.DecodeFile("config", &cnf)
    if err != nil {
        panic(err)
    }
    return cnf
}

现在,在我的主包中:

package main

import "./configuration"

var conf Configuration = configuration.Load()

会报错undefined: Config。我理解为什么会出错。我可以在主包中复制结构体定义,但这样做不够DRY(Don't Repeat Yourself)。

据我了解,像这样传递结构体是一种不好的做法,因为它会使你的代码更难理解(现在每个人都需要知道我的Config结构体)。

在Go语言中,像我在这里尝试做的这样隐藏逻辑的包是一个好主意吗?如果是的话,用什么方式在Go语言中传递这个Config结构体是最佳实践?

英文:

I am new to go and coming from a Ruby background. I am trying to understand code structuring in a world without classes and am probably making the mistake wanting to do it "the Ruby way" in Go.

I am trying to refactor my code to make it more modular / readable so I moved the loading of the configuration file to its own package. Good idea?

package configuration

import "github.com/BurntSushi/toml"

type Config struct {
    Temperatures []struct {
        Degrees int
        Units string
    }
}

func Load() Config {
    var cnf Config
    _, err := toml.DecodeFile("config", &cnf)
    if err != nil {
        panic(err)
    }
    return cnf
}

Now, in my main package:

package main

import "./configuration"

var conf Configuration = configuration.Load()

Gives undefined: Config. I understand why. I could copy the struct definition in the main package but that's not very DRY.

It's my understanding passing around structs like this is a bad practice as it makes your code harder to understand (now everyone needs to know about my Config struct).

Is hiding logic in a package like I am trying to do here a good idea in Go? If so, what's the "Go" way to pass this Config struct around?

答案1

得分: 4

在你的主包中,你应该指定:

var conf configuration.Config = configuration.Load()

configuration 是你导入的包,Config 是该包中导出的结构体(大写字母开头的名称)。但你也可以省略类型声明,因为类型可以被推断出来:

var conf = configuration.Load()

另外一点需要注意的是,请不要使用相对导入。

英文:

In your main package you should specify

var conf configuration.Config = configuration.Load()

configuration refers to your imported package and Config is the exported struct (uppercase name) from that package. But you can also omit this, as the type can be inferred

var conf = configuration.Load()

As a side note: please don't use relative imports

答案2

得分: 1

在Go语言中,你总是要声明包的完整路径,不要在导入中使用相对路径。最好的例子是导入toml包,例如import "github.com/BurntSushi/toml",它存在于以下路径中:
GOPATH/src/github.com/BurntSushi/toml
GOPATH/pkg/_/github.com/BurntSushi/toml

然后构建你的包和main.go文件:

package main

import "mypackage/configuration"

func main() {
   // configuration包含所有的函数和结构体
   var conf configuration.Config = configuration.Load()
}

Go语言不是Ruby。

参考文档:https://golang.org/doc/code.html

英文:

in Go imports you always declare the full path of you package, dont use relative paths in imports, best example is that toml import import "github.com/BurntSushi/toml" that exist in:
GOPATH/src/github.com/BurntSushi/toml
GOPATH/pkg/<OS>_<ARCH>/github.com/BurntSushi/toml

Then build you package and main.go

package main

import &quot;mypackage/configuration&quot;

func main() {
   // configuration contain all funcs &amp; structs 
   var conf configuration.Config = configuration.Load()
}

Go it is not ruby.

Ref Packages: https://golang.org/doc/code.html

答案3

得分: 1

为什么不直接导入配置包,然后使用Go的变量声明/实例化快捷方式呢?也许我漏掉了什么。

package main

import "mypackage/configuration"

func main() {
   conf := configuration.Load()
}
英文:

why don't you just import the configuration package and then do Go's variable declaration/instatiation shortcut? Maybe I'm missing something.

package main

import &quot;mypackage/configuration&quot;

func main() {
   conf := configuration.Load()
}

huangapple
  • 本文由 发表于 2016年2月6日 06:36:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/35234745.html
匿名

发表评论

匿名网友

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

确定