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. ...
随机推荐
- windows中Python多版本与jupyter notebook中使用虚拟环境
本人电脑是windows系统,装了Python3.7版本,但目前tensorflow支持最新的python版本为3.6,遂想再安装Python3.6以跑tensorflow. 因为看极客时间的专栏提到 ...
- LeetCode(103) Binary Tree Zigzag Level Order Traversal
题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...
- this version of SLF4J requires log4j version 1.2.12 or later.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFacto ...
- 电脑连接到手机并安装手机驱动usb-driver
设置真机开发环境需要执行下面几个步骤: 1.在应用的Manifest文件中声明应用是可调试的: 2.打开应用的调试支持: 对于通过Eclipse创建的应用,可以省略步骤2,因为在Eclipse IDE ...
- Python 前端 Css基础
CSS样式存在的位置 1.放置在标签内,局部生效 <div style="color: red;font-size: 18px;">hello world</di ...
- loj2274 「JXOI2017」加法
二分一下,然后从左到右扫描,扫到左端点就把区间 push 到堆里. 每次有点不符合二分的值时,就贪心地选择右端点最远的 add. #include <algorithm> #include ...
- 关于面试总结-app测试面试题
前言 现在面试个测试岗位,都是要求全能的,web.接口.app啥都要会测,那么APP测试一般需要哪些技能呢? 面试app测试岗位会被问到哪些问题,怎样让面试管觉得你对APP测试很精通的样子? 本篇总结 ...
- BNUOJ 1055 走迷宫2
走迷宫2 Time Limit: 1000ms Memory Limit: 65535KB 64-bit integer IO format: %lld Java class name: ...
- pytorch中torch.unsqueeze()函数与np.expand_dims()
numpy.expand_dims(a, axis) Expand the shape of an array. Insert a new axis that will appear at the a ...
- POJ 1606 Jugs
Jugs Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4280 Accepted: 2533 Special Ju ...
go
