golang 中全局变量的问题。

------------------------------------------------------------------

I'm fairly new to golang, this should be a simple answer but I've tried searching every where with no luck.

How do I access a global 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

asked Jan 27 '16 at 13:36
Nighthee

1831110

2 Answers

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
answered Jan 27 '16 at 13:57
olif

1,33011519

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!

answered Jan 27 '16 at 14:02
  •  
    My 'dif' is a restful API and won't be called when the program starts but only when its invoked through URL, so i need to put the StartTime in main.go and pass it to dif, you're passing it from dif to main, thanks for the try though! – Nighthee Jan 27 '16 at 14:05

Golang Global Variable access的更多相关文章

  1. Can we access global variable if there is a local variable with same name?

    In C, we cannot access a global variable if we have a local variable with same name, but it is possi ...

  2. robot framework 上个用例的输出作为下个用例的输入 (Set Global Variable的用法)

    变量的作用域 通常情况下,每个变量默认都是局部变量. 一个case里的变量,作用域在这个case内部: 一个userkeyword里的变量,作用域在这个userkeyword内部: 一个文件型suit ...

  3. USE " cc.exports.* = value " INSTEAD OF SET GLOBAL VARIABLE"

    Cocos2d-x 3.5的lua项目生成后,变成了MVC模式,并且,加入了一个全局变量的检测功能.也就是说,你不小心用了全局变量,他会提示你出错! 比如 local temp = 1 temp = ...

  4. Use of implicitly declared global variable

    https://stackoverflow.com/questions/7604419/resharper-javascript-use-of-implicitly-declared-global-v ...

  5. Global variable in ABAP function group

    Function group is loaded into runtime memory by the FIRST call of a function module inside this func ...

  6. CodeIgniter 定义“全局变量-global variable”,可以在所有controller,model和view中使用

    本文抄自http://www.cnblogs.com/webu/archive/2012/11/20/2779999.html 第一次正儿八经用CodeIgniter框架做项目,结果不会定义全局变量, ...

  7. golang ODBC 访问access数据库(问题解决之心理路程)

    最近项目需要,需要操作access,以前是用VC++ OLE访问,网络用ACE库,感觉很庞大...决定用go试试 网上用的最多的就是这个https://github.com/weigj/go-odbc ...

  8. 【兼容调试】cffi library '_openssl' has no function, constant or global variable named 'Cryptography_HAS

    重装cryptography就好了. conda uninstall cryptography conda install cryptography https://github.com/pyca/c ...

  9. Global & Local Variable in Python

    Following code explain how 'global' works in the distinction of global variable and local variable. ...

随机推荐

  1. Python从文件中读取数据

    一.读取整个文件内容 在读取文件之前,我们先创建一个文本文件resource.txt作为源文件. resource.txt my name is joker, I am 18 years old, H ...

  2. DFS:POJ1562-Oil Deposits(求连通块个数)

    Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Description The GeoSurvComp geologic survey com ...

  3. 经典DP问题--poj1088滑雪

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  4. LA 4256 DP Salesmen

    d(i, j)表示使前i个数满足要求,而且第i个数值为j的最小改动次数. d(i, j) = min{ d(i-1, k) | k == j | G[j][k] } #include <cstd ...

  5. 【02】markdown工具推荐

    [02]信息 Windows 平台 MarkdownPad MarkPad Linux 平台 ReText Mac 平台 Mou 最新版Mac OS下Mou已经无法使用了.这里推荐一个跨平台的编辑器  ...

  6. xfce-openvas9

    1安装OpenVas 第一步,添加PPA源,在这我用的是一台新装的Ubuntu安装OpenVas,运行以下命令就可以进行安装 root@ubuntu:~# add-apt-repository ppa ...

  7. CSU-1803: 2016 ,同余定理!

    int main() {     int n,m;     while(~scanf("%d%d",&n,&m))     {         int r1=n/2 ...

  8. 洛谷P2351 [SDOi2012]吊灯 【数学】

    题目 Alice家里有一盏很大的吊灯.所谓吊灯,就是由很多个灯泡组成.只有一个灯泡是挂在天花板上的,剩下的灯泡都是挂在其他的灯泡上的.也就是说,整个吊灯实际上类似于[b]一棵树[/b].其中编号为 1 ...

  9. 【Luogu】P1972HH的项链(链表+树状数组)

    题目链接 难题,所以会讲得细一些. 首先我们想如何统计区间[l,r]内不同贝壳的个数. 第一个思路就是线段树/树状数组,query(1,r)-query(1,l-1)对不对? 然而这样是不对的. 然后 ...

  10. Codeforces 333E Summer Earnings ——Bitset

    [题目分析] 找一个边长最大的三元环. 把边排序,然后依次加入.加入(i,j)时,把i和j取一个交集,看看是否存在,存在就找到了最大的三元环. 输出即可,n^3/64水过. [代码] #include ...