Go包中的全局变量在导入中的使用

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

Go package Global Variable usage within imports

问题

我正在创建一个包,将从我在Go中拥有的其他服务中调用。如果一个服务导入了这个包,并且这个包有全局变量,那么组件会将全局变量存储在自己的内存中吗?

所以,如果我的包看起来像这样:

var global1 = ""
var global2 = ""
var global3 = ""
var global4 = ""

func setAllGlobalValues() error {
  // 一些逻辑,检查全局变量是否为nil
  // 如果不是,在一些计算后将其设置为一个值。
  // 返回nil或实际错误。
}

func DoesSomethingUsingGlobalVars() (bool, error) {
  // 设置并使用全局变量。
  // 进行某种计算并返回一个布尔值,nil或nil,错误
}

然后在服务中,我将导入这个包并使用DoesSomethingUsingGlobalVars函数。
使用这个包的组件会将全局变量存储在自己的内存中吗?我现在无法通过我的服务进行测试,因为设置的方式不同,所以我只是好奇是否有人知道。

基本上,这样会起作用吗?还是每次从导入这个包的服务中调用任何东西时,全局变量都会是nil?

提前感谢大家!

英文:

I am creating a package that will be called from other services that I have in Go. If a service imports this package, and this package has global variables, will the component store the global variables in its' own memory?

So if my package looks something like this,

var global1 = ""
var global2 = ""
var global3 = ""
var global4 = ""

func setAllGlobalValues() error {
  // some logic that checks if globals are nil
  // if not setting it to a value after some computation.
  // returns nil or an actual error.
}

func DoesSomethingUsingGlobalVars() (bool, error) {
  // sets and uses the global vars.
  // Does some sort of computation and returns a bool, nil or nil,error
}

Then in the service I would import this package and use the doesSomethingUsingGlobalVars function.
Will the component using this package store the global variables in its own memory? I can't really test it now with my services with the way things are set up so I'm just curious if anyone knows.

Essentially, will this work or will the global vars be nil each and every-time anything is called from a service that imports this package?

Thanks in advance all!

答案1

得分: 1

当你的程序导入这个包时,当你调用SetAllGlobalValues()时,全局变量将被设置,稍后当你调用DoesSomethingUsingGlobalVars()时,这些值将已经被设置。请注意,这些函数名的首字母必须大写,以便它们被导出并可供其他包使用。如果变量没有被导出,就像你的代码片段中所示,你将无法直接从调用者访问它们。

英文:

When your program imports this package, the globals will be set when you call SetAllGlobalValues(), and later, when you call DoesSomethingUsingGlobalVars(), those values will already be set. Note that the first letter of those function names must be capitalized so that they are exported and available for use by other packages. If the variables are not exported, as shown in your code snippet, you will not be able to access them directly from the caller.

答案2

得分: 1

似乎你正在尝试重新定义对象。不要使用你的代码,而是像这样做:

package some
import "fmt"

type Thing struct {
   One string
   Two string
   Three string
   Four string
}

func NewThing() Thing {
   return Thing{"One", "Two", "Three", "Four"}
}

func (t Thing) Print() {
   fmt.Println(t.One, t.Two, t.Three, t.Four)
}

然后,"全局变量"只在调用NewThing时计算一次。

英文:

It seems as if you are trying to reinvent objects. Instead of your code, do
something like this:

package some
import "fmt"

type Thing struct {
   One string
   Two string
   Three string
   Four string
}

func NewThing() Thing {
   return Thing{"One", "Two", "Three", "Four"}
}

func (t Thing) Print() {
   fmt.Println(t.One, t.Two, t.Three, t.Four)
}

Then, the "global variables" are only calculated once, when you call NewThing.

huangapple
  • 本文由 发表于 2021年7月8日 05:40:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/68293248.html
匿名

发表评论

匿名网友

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

确定