如何访问全局变量

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

How to access global variables

问题

你可以通过在不同的.go文件中使用包级别的变量来访问在main.go中声明/初始化的变量。确保在main.go文件中将StartTime声明为包级别的变量,即在函数外部声明它。然后,在其他.go文件中导入main包,并使用main.StartTime来访问该变量。这样就可以避免出现"StartTime undefined"的错误。以下是示例代码:

在main.go中:

package main

import (
    "fmt"
    "time"
)

var StartTime = time.Now()

func main() {
    // ...
}

在其他.go文件中:

package otherpackage

import (
    "fmt"
    "path/to/main"
)

func someFunction() {
    fmt.Println(main.StartTime)
}

通过这种方式,你就可以在其他.go文件中访问并使用StartTime变量了。

英文:

How do I access a variable that was declared/init in my main.go in a different .go package/file? Keeps telling me that the variable is undefined (I know that global variables are bad but this is just to be used as a timestamp)

in main.go

var StartTime = time.Now()
func main(){...}

trying to access StartTime in a different .go file
but keep getting StartTime undefined

答案1

得分: 110

我会将 "starttime" 变量作为 "注入",否则包之间会存在循环依赖关系。

main.go

var StartTime = time.Now()
func main() {
   otherPackage.StartTime = StartTime
}

otherpackage.go

var StartTime time.Time
英文:

I would "inject" the starttime variable instead, otherwise you have a circular dependency between the packages.

main.go

var StartTime = time.Now()
func main() {
   otherPackage.StartTime = StartTime
}

otherpackage.go

var StartTime time.Time

答案2

得分: 18

我创建了一个名为dif.go的文件,其中包含你的代码:

package dif

import (
	"time"
)

var StartTime = time.Now()

在文件夹外部,我创建了一个名为main.go的文件,一切正常!

package main

import (
	dif "./dif"
	"fmt"
)

func main() {
	fmt.Println(dif.StartTime)
}

输出结果:

2016-01-27 21:56:47.729019925 +0800 CST

文件目录结构:

folder
  main.go
  dif
    dif.go
它可以正常工作!
英文:

I create a file dif.go that contains your code:

package dif

import (
	"time"
)

var StartTime = time.Now()

Outside the folder I create my main.go, it is ok!

package main

import (
	dif "./dif"
	"fmt"
)

func main() {
	fmt.Println(dif.StartTime)
}

Outputs:

2016-01-27 21:56:47.729019925 +0800 CST

Files directory structure:

folder
  main.go
  dif
    dif.go

It works!

答案3

得分: 0

我建议使用常见的导入方式。

首先,我将解释一下被称为“相对导入”的方式,也许这种方式会导致一些错误。

其次,我将解释常见的导入方式。

首先:

在 Go 版本 >= 1.12 中,关于导入文件有一些新的提示,一些东西也发生了变化。

1- 你应该将你的文件放在另一个文件夹中,例如我在 "model" 文件夹中创建了一个名为 "example.go" 的文件。

2- 当你想要导入一个文件时,你必须使用大写字母。

3- 对于你想要在其他文件中导入的变量、结构体和函数,使用大写字母。

注意:没有办法在另一个文件中导入 main.go。

文件目录如下:

root
|_____main.go
|_____model
          |_____example.go

这是一个 example.go:

package model

import (
    "time"
)

var StartTime = time.Now()

这是 main.go,当你想要导入一个文件时,你应该使用大写字母。"Mod" 以大写字母开头。

package main

import (
    Mod "./model"
    "fmt"
)

func main() {
    fmt.Println(Mod.StartTime)
}

注意!!!

注意:我不推荐这种类型的导入!

其次:

(普通导入)

更好的导入文件的方式是:

你的结构应该像这样:

root
|_____github.com
         |_________你在 GitHub 上的账户名
         |                |__________你的项目名
         |                                |________main.go
         |                                |________handlers
         |                                |________models
         |               
         |_________gorilla
                         |__________sessions

这是一个示例:

package main

import (
    "github.com/gorilla/sessions"
)

func main(){
     //在这里你可以使用 sessions
}

所以你可以在任何你想要的地方导入 "github.com/gorilla/sessions",只需导入它即可。

英文:

I suggest use the common way of import.

First I will explain the way it called "relative import" maybe this way cause of some error

Second I will explain the common way of import.

FIRST:

In go version >= 1.12 there is some new tips about import file and somethings changed.

1- You should put your file in another folder for example I create a file in "model" folder and the file's name is "example.go"

2- You have to use uppercase when you want to import a file!

3- Use Uppercase for variables, structures and functions that you want to import in another files

Notice: There is no way to import the main.go in another file.

file directory is:

root
|_____main.go
|_____model
          |_____example.go

this is a example.go:

package model

import (
    "time"
)

var StartTime = time.Now()

and this is main.go you should use uppercase when you want to import a file. "Mod" started with uppercase

package main

import (
    Mod "./model"
    "fmt"
)

func main() {
    fmt.Println(Mod.StartTime)
}

NOTE!!!

NOTE: I don't recommend this this type of import!

SECOND:

(normal import)

the better way import file is:

your structure should be like this:

root
|_____github.com
         |_________Your-account-name-in-github
         |                |__________Your-project-name
         |                                |________main.go
         |                                |________handlers
         |                                |________models
         |               
         |_________gorilla
                         |__________sessions
                        

and this is a example:

package main

import (
    "github.com/gorilla/sessions"
)

func main(){
     //you can use sessions here
}

so you can import "github.com/gorilla/sessions" in every where that you want...just import it.

huangapple
  • 本文由 发表于 2016年1月27日 21:36:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/35038864.html
匿名

发表评论

匿名网友

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

确定