如何在Go语言中覆盖嵌套包中的变量?

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

How to override variables from nested packages in go language?

问题

如何在Go语言中从包中覆盖变量?

例如:

file1.go

package A

import "A/B"

var Arg1 = "Hello"

file2.go

package A/B

var Arg1 = "World"

如果在file2.go中存在arg1,我该如何在file1.go中覆盖arg1?

英文:

How do I override variables in go from package ?

For example:

file1.go

package A

import "A/B"

var Arg1 = "Hello"

file2.go

package A/B

var Arg1 = "World"  

How can I override arg1 in file1.go if arg1 exist in file2.go?

答案1

得分: 3

我不确定你在这种情况下所指的“覆盖”的意思。 (还让我假设file1.go是“包a”,file2.go是“包b”)

如果你的意思是在“包a”内部/从“包a”中访问在“包b”中定义的Arg1,那么可以这样做:

// 在“包a”中
x = Arg1    // 引用a.Arg1
y = b.Arg1  // 引用b.Arg1

然而,在这里并没有发生类似覆盖的情况。在“包a”中,a的Arg1和b的Arg1都作为不同的实体可访问;后者需要使用强制限定符“b”。

英文:

I'm not sure what you mean by "overriding" in this case. (Also let me assume file1.go is 'package a' and file2.go is 'package b')

If you mean to access Arg1, defined in package "b" inside/from within package "a", then eg.:

// In package "a"
x = Arg1    // references a.Arg1
y = b.Arg1  // references b.Arg1

However, nothing resembling overriding happens here. In package "a" both a's Arg1 and b's Arg1 are accessible as different entities; the later via a mandatory qualifier 'b'.

答案2

得分: 1

你不能。包是自包含的。
如果包A不导出arg1(小写)
它是不可见的 - 因此也不能设置 -
对于另一个包B。

顺便说一句:“包A/B”对我来说看起来很奇怪。

英文:

You can't. Packages are self-contained.
If package A doesn't export arg1 (lowercase)
it is not visible -- and thus not set-able --
for an other package B.

BTW: "package A/B" looks pretty strange to me.

答案3

得分: 1

你是否试图做类似这样的事情,例如,如果存在特定位置(美国),则覆盖一般位置(世界)?

// 文件 A/B.go

package B

var location = "USA"

func Location() string {
    return location
}

// 文件 A.go

package A

import "A/B"

var location = "World"

func Location() string {
    loc := B.Location()
    if len(loc) == 0 {
        loc = location
    }
    return loc
}

// 文件 main.go

package main

import (
    "A"
    "fmt"
)

func main() {
    fmt.Println("Hello, ", A.Location())
}

输出:

Hello, USA

如果不是,请提供一个更好和具体的示例,说明你想要做什么。

英文:

Are you trying to do something like this, where, for example, a specific location (USA), if present, overrides a general location (World)?

// file A/B.go

package B

var location = "USA"

func Location() string {
	return location
}

// file A.go

package A

import "A/B"

var location = "World"

func Location() string {
	loc := B.Location()
	if len(loc) == 0 {
		loc = location
	}
	return loc
}

// file main.go

package main

import (
	"A"
	"fmt"
)

func main() {
	fmt.Println("Hello,", A.Location())
}

Output:

Hello, USA

If not, please provide a better and specific example of what you are trying to do.

答案4

得分: 0

你可以使用构建标志来完成这个任务:

go build -ldflags="-X 'package_path.variable_name=new_value'"

但是需要注意以下几点:

  • 为了使用ldflags,你想要改变的值必须存在,并且是一个字符串类型的包级变量。这个变量可以是导出的或者未导出的。这个值不能是常量,也不能通过函数调用的结果来设置。

你可以在这篇来自DO团队的优秀文章中找到所有需要的信息。

英文:

You could probably do it using a build flag :

go build -ldflags="-X 'package_path.variable_name=new_value'"

Couple of things to be aware of though :

In order to use ldflags, the value you want to change must exist and be a package level variable of type string. This variable can be either exported or unexported. The value cannot be a const or have its value set by the result of a function call

You'll find all the information needed in this excellent post from DO team

huangapple
  • 本文由 发表于 2013年4月17日 15:06:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/16053678.html
匿名

发表评论

匿名网友

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

确定