大家好,我是 Go 的新手,我正在编写一个简单的应用程序,它从 env 变量中获取一些配置。我在 init 函数中执行此操作,如下所示。
type envVars struct {
Host string `env:"APP_HOST"`
Username string `env:"APP_USERNAME"`
Password string `env:"APP_PASSWORD"`
}
var envConfig envVars
func init() {
if err := env.Parse(&envConfig); err != nil {
log.Fatal(err)
}
}
我写了测试来验证环境变量是否被正确读取。但问题是我的程序的 init func 甚至在我的测试的 init func 之前就被调用了。在我的程序的 init func 被调用之前,有什么方法可以做一些设置。
func init() {
os.Setenv("APP_HOST", "http://localhost:9999")
os.Setenv("APP_USERNAME", "john")
os.Setenv("APP_PASSWORD", "doe")
}
func TestEnvConfig(t *testing.T) {
assert.NotNil(t, envConfig)
assert.Equal(t, "http://localhost:9999", envConfig.Host)
}
最佳答案
您可以使用 TestMain 函数来控制测试前后发生的事情。
例如:
func TestMain(m *testing.M) {
// Write code here to run before tests
// Run tests
exitVal := m.Run()
// Write code here to run after tests
// Exit with exit value from tests
os.Exit(exitVal)
}
func TestYourFunc(t *testing.T) {
// Test code
}
关于戈朗 : Testing with init() func,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38484942/
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
我想实现以下目标:构建一个Ruby命令行实用程序来注册一些set_trace_func事件,然后调用您传递给它的任何ruby可执行参数(比如rspec)。注册的事件然后转移到调用的命令。myutility的伪代码:set_trace_func()#Setsomeeventshereexec(ARGV.join(''))#Executeargumentpassed然后调用myutilityrspec。我的目标是实际在任意命令上注册跟踪点(只要它们使用ruby垫片)。我尝试过的事情:exec不起作用,原因很明显(它完全取代了进程)。popen、系统、反引号。这些启动了一个独立的过程
我正在尝试设置一个在每次页面加载时调用的全局函数,无论它在我的网站中的位置如何。根据Express的API,我使用了app.all("*",doSomething);在每次加载页面时调用函数doSomething,但它并不完全有效。该函数在每次页面加载时触发,除了基本域的页面加载(例如http://domain.com/pageA将调用该函数,但http://domain.com不会)。有谁知道我做错了什么?谢谢! 最佳答案 我打赌你放了app.get('/',fn)以上app.all("*",doSomething);请记住,Ex
我已经使用Function.prototype.func=...添加了一个函数到Function但在Firefox中它没有被添加console.log:Function.prototype.func=function(){returnthis.toString();};alert(typeofconsole.log.func);//inFF:undefined,inChrome:function这是错误还是有任何原因? 最佳答案 在Firefox中很明显:varfoo=function(){}foo.__proto__==Funct
这个问题在这里已经有了答案:Doyoueverneedtospecify'javascript:'inanonclick?(8个答案)关闭8年前。有什么区别吗和我应该在JS函数调用之前使用javascript:吗?
为什么下面的代码会抛出意外的函数错误?我看到错误./func_correct.go:4:syntaxerror:unexpectedfunc,expectingnamepackagemainfunc(st*Stack)Pop()int{v:=0forix:=len(st)-1;ix>=0;ix--{ifv=st[ix];v!=0{st[ix]=0returnv}}return0}funcmain(){Pop()} 最佳答案 定义堆栈类型在main中为其创建一个变量对其调用Pop代码:packagemainimport"fmt"typ
这个问题在这里已经有了答案:Functiondeclarationsyntax:thingsinparenthesisbeforefunctionname(3个答案)WhatsthedifferenceoffunctionsandmethodsinGo?(5个答案)ParameterbeforethefunctionnameinGo?[duplicate](1个回答)WhatdothebracketsafterfuncmeaninGo?[duplicate](1个回答)关闭8个月前。我正在学习Golang-在教程中我经常看到这样的语法:typeSomeTypestruct{//stru
我正在学习golang,对于将一个函数作为参数传递给另一个函数的代码,我不知道我列出的代码的含义对于quote123函数,它需要一个函数作为参数,如何将部分:func(xint)string{returnfmt.Sprintf("%b",x)}传递给quote123函数,即使这样有效,如果那部分返回一个字符串,这个字符串不应该是函数quote123的参数//converttypestakeanintandreturnastringvalue.typeconvertfunc(int)string//valueimplementsconvert,returningxasstring.fun
当我使用它在结构上迭代时,内存地址是不同的。所以我不能修改它的值没有人typeSiteUrlstruct{namestringurlstringisUpbool}funcdebug(s*SiteUrl){s.isUp=false}funcmain(){sites:=[]SiteUrl{{"testsite","http://127.0.0.1:8000",true},}for{for_,site:=rangesites{fmt.Println(&site.isUp,site.isUp)debug(&site)}}}它的值没有修改 最佳答案
关闭。这个问题需要更多focused.它目前不接受答案。想改进这个问题吗?更新问题,使其只关注一个问题editingthispost.关闭3年前。Improvethisquestion告诉我如何使用Golang登录网站。下载xls文件是得到了,但是为了在Excel表格中有数据,需要登录网站。该站点位于公司的服务器上。如果你能告诉你怎么做。例如,我用来执行此操作的VBA代码。SetoFields=CreateObject("Scripting.Dictionary")WithoFields.Add"login","sdiscor".Add"password","sdiscor"EndWi