Go : Same name and content struct in one package , which one will be initialised

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

Go : Same name and content struct in one package , which one will be initialised

问题

mount_mac.go中的Mounter结构体会被初始化,是因为在mount_test.go中的TestNew函数中调用了New()函数,而New()函数会返回一个Mounter结构体的实例。因此,每次运行TestNew函数时,都会初始化一个新的Mounter实例。

英文:

There is a package named mount, which has two same name and content structs

> mount_linxu.go

package mount
import "fmt"
type Mounter struct {
}
func (mounter *Mounter) DoMount(path string) (bool ,error){
fmt.Printf("this is linux")
return true , nil
}

> mount_mac.go

package mount
import "fmt"
type Mounter struct {
}
func (mounter *Mounter) DoMount(path string) (bool ,error){
fmt.Printf("this is mac")
return true , nil
}

> mount_test.go

func TestNew(t *testing.T) {
 mounter := New()
 mounter.DoMount("")
}

My Question is why Mounter in mount_mac.go will be initialised always?

答案1

得分: 1

如所示,这些文件无法编译,因为你在同一个包中声明了两次Mounter

使其工作的选项有:

1)为每个文件使用两个不同的包,例如:

/src/components/mac/mount_mac.go

/src/components/linux/mount_linux.go

然后,在每种情况下导入所需的包。

2)使用条件构建,这样根据你所针对的平台,只有一个文件会被包含进来。
你可以在每个文件中使用// +build标签。

在这里了解更多信息:https://golang.org/pkg/go/build/#hdr-Build_Constraints

英文:

As presented, those files won't compile since you are declaring Mounter two times within the same package.

Your options to make this work are:

  1. Use two different packages for each of those files, like:

/src/components/mac/mount_mac.go

/src/components/linux/mount_linux.go

Then, import the package you need in each case.

  1. Use conditional build so that depending on the platform you are targeting, only one of the files is included.
    You can use a // +build tag in each file.

See more about that here: https://golang.org/pkg/go/build/#hdr-Build_Constraints

答案2

得分: 1

如果你使用正确的文件后缀,它们将根据操作系统进行条件构建。Go语言不将MacOS称为"mac",而是将其称为"darwin"(Mac内核)。它也正确拼写"linux",尽管我不知道这是否只是问题中的拼写错误,或者你的磁盘上的文件名也有相同的拼写错误。另外,你可以使用显式的构建约束而不是文件名,但是文件名在针对特定操作系统或架构的代码中是惯用的。

如果结构体定义相同,你可以将结构体放在一个通用的mount.go文件中,然后只需将特定于操作系统的方法定义放在特定于操作系统的文件中,以避免不必要的重复代码。

英文:

If you use the correct file suffixes, they will be conditionally built based on OS. Go does not refer to MacOS as "mac", it refers to it as "darwin" (the Mac kernel). It also correctly spells "linux", though I don't know if that was just typo'd in the question or if your file name on disk has the same typo. Alternatively, you can use explicit build constraints rather than file names, but the file names are idiomatic for OS- or architecture-specific code.

If the struct definitions are identical, you can put the struct in a general mount.go file, and just put the OS-specific method definitions in the OS-specific files to avoid unnecessary duplicate code.

huangapple
  • 本文由 发表于 2017年6月24日 06:57:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/44730995.html
匿名

发表评论

匿名网友

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

确定