Golang Global Variable access
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
- 2
- Is the first letter on the variable name capitalized? – olif Jan 27 '16 at 13:43
- Yes, it is capitalized, and my go build fails – Nighthee Jan 27 '16 at 13:44
- are you including all relevant files when running the program? That is, when running the code, are you running go run file1.go file2.go ..etc – olif Jan 27 '16 at 13:45
- Yes, to be more concise, I have a variable called 'var StartTime = time.Now()' in my main.go But when i try to access StartTime in a different .go of the same directory, it says its undefined, would i have to include "main" in the .go file that im trying to call? – Nighthee Jan 27 '16 at 13:47
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
- Yep it worked! Thanks! – Nighthee Jan 27 '16 at 14:34
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!
- 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的更多相关文章
- 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 ...
- robot framework 上个用例的输出作为下个用例的输入 (Set Global Variable的用法)
变量的作用域 通常情况下,每个变量默认都是局部变量. 一个case里的变量,作用域在这个case内部: 一个userkeyword里的变量,作用域在这个userkeyword内部: 一个文件型suit ...
- USE " cc.exports.* = value " INSTEAD OF SET GLOBAL VARIABLE"
Cocos2d-x 3.5的lua项目生成后,变成了MVC模式,并且,加入了一个全局变量的检测功能.也就是说,你不小心用了全局变量,他会提示你出错! 比如 local temp = 1 temp = ...
- Use of implicitly declared global variable
https://stackoverflow.com/questions/7604419/resharper-javascript-use-of-implicitly-declared-global-v ...
- Global variable in ABAP function group
Function group is loaded into runtime memory by the FIRST call of a function module inside this func ...
- CodeIgniter 定义“全局变量-global variable”,可以在所有controller,model和view中使用
本文抄自http://www.cnblogs.com/webu/archive/2012/11/20/2779999.html 第一次正儿八经用CodeIgniter框架做项目,结果不会定义全局变量, ...
- golang ODBC 访问access数据库(问题解决之心理路程)
最近项目需要,需要操作access,以前是用VC++ OLE访问,网络用ACE库,感觉很庞大...决定用go试试 网上用的最多的就是这个https://github.com/weigj/go-odbc ...
- 【兼容调试】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 ...
- Global & Local Variable in Python
Following code explain how 'global' works in the distinction of global variable and local variable. ...
随机推荐
- Python变量、常量、数据类型
1. 变量 变量是一种使用方便的占位符,用于引用计算机内存地址,该地址可以存储Script运行时可更改的程序信息.例如,可以创建一个名为ClickCount的变量来存储用户单击Web页面上某个对象的次 ...
- 【php】类型转换
$a = 9; print_r((array) $a) ; 输出: [0=>9] print_r((array) null); 输出: []
- Web框架之Django_03 路由层了解(路有层 无名分组、有名分组、反向解析、路由分发 视图层 JsonResponse,FBV、CBV、文件上传)
摘要: 路由层 无名分组 有名分组 反向解析 路由分发 名称空间 伪静态网页.虚拟环境 视图层 JsonResponse FBV 与 CBV(function base views与class bas ...
- centos 装 jdk
1.源码包准备: 首先到官网下载jdk,http://www.oracle.com/technetwork/java/javase/downloads/jdk7- downloads-1880260. ...
- HDU-3743 Minimum Sum,划分树模板
Minimum Sum 被这个题坑了一下午,原来只需找一个最中间的数即可,我以为是平均数. 题意:找一个数使得这个数和区间内所有数的差的绝对值最小.输出最小值. 开始用线段树来了一发果断T了,然后各种 ...
- 刷题总结——mayan游戏(NOIP2011提高组day2T3)
题目: 题目背景 NOIP2011提高组 DAY1 试题. 题目描述 Mayan puzzle 是最近流行起来的一个游戏.游戏界面是一个 7 行 5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即 ...
- 刷题总结——蚯蚓(NOIP2016DAY2T2)
题目: 题目背景 NOIP2016 提高组 Day2 T2 题目描述 本题中,我们将用符号
- SHUoj 神无月排位赛
神无月排位赛 发布时间: 2017年7月8日 21:06 最后更新: 2017年7月8日 22:35 时间限制: 1000ms 内存限制: 128M 描述 <神无月>作为盛大游 ...
- RRAS
远程访问控制是Windows NT.Win200x Server提供的一种远程服务,它允许用户从远端通过拨号连接连接到一个本地的计算机网络,一旦建立了连接,就相当于处在了本地的LAN中,从而可以使用各 ...
- 前端开发之html篇
一.什么是html? 1.我们说socket网络编程的时候,提到过一个cs模型,就是客户端—服务端模型,前端开发也是基于网络编程,但是这时就应该是bs模型了,是浏览器与服务端的通信. 我们可以模拟一个 ...