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. 文件操作-touch

    本文来给大家介绍另外一个比较常用的命令--touch命令,Linux touch命令 主要用来修改文件或者目录的时间属性,或者建立新文件. 转载自https://www.linuxdaxue.com/ ...

  2. linux-MySQL基本指令-增删改查

    常用指令 指令作用 指令 查看/查询 show,select,desc 创建 create 删除 drop,delete,truncate 切换/进入 use 添加记录 insert 查询类 查看数据 ...

  3. laravel中对加载进行优化

    在laravel中的模型与模型之间创建好关联关系会比较方便的方法 但是我们为了方便,有时也会忽略一些东西,比如: 我们在控制器中把整个一个文章对象传到了模板页面 在一次for循环下, 我们对数据进行了 ...

  4. python的web框架---Django项目

    Django项目之会议室预预订: 界面效果展示: 1.创建超级管理员,实现预定界面功能 2.预定界面: (一)基于pymysql设计数据表结构,理清前后端与用户交互逻辑.(用户表,会议室表,预定内容存 ...

  5. jq相关操作

    1事件: <div class="ele">123</div> box.onclick = function(ev){ ev:系统传入的事件对象 ele.i ...

  6. Day10文件内指针移动和函数

    强调:只有t模式下的read(n),n代表字符个数,除此以外都是以字节为单位 ,例如f.read(4)读出4个字符 控制文件内指针的移动:f.seek()以字节为单位 f.tell()文件开头为准,当 ...

  7. 洛谷 P4961

    目录 题目 思路 Code 题目 戳 为了小埋A了这道题. 思路 读入原来的矩阵,将不是雷的格子更新为数字(数字就是该格子周围八格的雷的个数)将是雷的格子赋值为inf.然后就按照题目要求计算周围八格没 ...

  8. SQL 优化案例之变更表结构

    从慢日志报表中看到一条很长的SQL select id from myinfo and (( SUBSTRING_INDEX(location_axis, ) ) ) ), '$%') ) or ( ...

  9. 集群高可用之lvs

    集群: 随着互联网的发展,大量的客户端的请求,服务器的负载越来越大,单台服务器的负载有限,会导致服务器响应客户端请求的时间越长,甚至产生拒绝服务的情况.目前网站是24小时不间断提供网络服务,仅采用单点 ...

  10. cell展开的几种方式

    一.插入新的cell 原理: (1)定义是否展开,和展开的cell的下标 @property (assign, nonatomic) BOOL isExpand; //是否展开 @property ( ...