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. Mac OSX: 有线共享WiFi

    首先连上有线 系统偏好设置->网络->点击左侧WiFi,再点击右下角[高级] 勾选[创建电脑对电脑网络],然后单击[好] 在顶部菜单栏击WiFi图标,如果WiFi未打开,则单击打开,如果已 ...

  2. NFS基础优化

    一.环境 环境接上篇 https://www.cnblogs.com/suffergtf/p/9486250.html 二.参数详解 1.nfsserver端配置参数详解 [root@nfsserve ...

  3. (原)iOS 用recursiveDescription打印View

    今天要做一个搜索功能,用到UISearchBar 无奈背景太丑,就自定义了一个,首先用View私有方法打印一下searchBar的层次, 具体修改代码如下 for (UIView *view in _ ...

  4. shell相关指令介绍$*和$#以及$?和if [[ ! -z $1 ]]

    $#,脚本运行时后跟的参数个数 #! /bin/bash case "$#" in 0) printf "Enter a number: " read n=$R ...

  5. Educational Codeforces Round 31- D. Boxes And Balls

    D. Boxes And Balls time limit per test2 seconds memory limit per test256 megabytes 题目链接:http://codef ...

  6. python基础学习笔记——内置函数

    一. 简介 python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:点击查看, 为了方便查看,将内置函数的总结记录下来. 二. 使用说明 以下是Python3版本所有的内 ...

  7. luogu2569 [SCOI2010]股票交易

    题解看这里 #include <iostream> #include <cstring> #include <cstdio> using namespace std ...

  8. 九度oj 题目1019:简单计算器

    题目描述:     读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. 输入:     测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之 ...

  9. 关于ul中li不对齐的问题

    将li中加入 overflow:hidden;    即可. 同时overflow:auto  可以控制滚动条的出现.

  10. P2014 选课 (树形动规)

    题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...