Passing context to gorilla mux

I'm reasonably new to golang and am trying to do work out the best way to do this idiomatically. I have an array of routes I am statically defining and passing to gorilla/mux . I am wrapping each handler function with something to time the request and handle panics (mainly so I could understand how the wrapping worked). I want them each to be able to have access to a 'context' -


我对golang来说是个新手,并且试图找出最好的方式来实现这一点。 我有一组静态定义并传递给gorilla/mux的路由。 我用一些东西来包装每个处理函数来处理请求并处理恐慌(主要是我可以理解包装是如何工作的)。 我希望他们每个人都能够访问一个'上下文' - 一个结构,这将是一个每http服务器,可能有像数据库句柄,配置等事情。我不想做的是使用一个静态全局变量。 我现在正在做这件事的方式,我可以让包装访问上下

Who does the garbage collection work in go?

According to this specification, there is a mark-and-sweep garbage collection mechanism behind go. But who does it? Go code will compile to native binary, right? So there will not be a virtual machine like Java which it could rely on. So who does this dirty work for us? A cryptic thread? Or just a goroutine? Will the garbage collecting procedure stop-the-world like a full GC of Java? An


根据这个规范,在后面有一个标记和扫描垃圾收集机制。 但是谁呢? Go代码将编译为本机二进制文件,对吗? 所以不会有像Java这样可以依赖的虚拟机。 那么,谁为我们做这种肮脏的工作? 神秘的线程? 或者只是一个goroutine? 垃圾收集过程是否像Java的完整GC一样停止了世界 ? 任何人都可以说出Java和Go之间的GC机制的区别吗? 我可以在网上找到很少的材料。 你的许多问题在这里得到解答: Go使用哪种垃圾收集?

compiled binary won't run in an alpine docker container on Ubuntu host

Given a binary, compiled with Go using GOOS=linux and GOARCH=amd64 , deployed to a docker container based on alpine:3.3 , the binary will not run if the docker engine host is Ubuntu (15.10): sh: /bin/artisan: not found This same binary (compiled for the same OS and arch) will run just fine if the docker engine host is busybox (which is the base for alpine ) deployed within a VirtualBox VM on Ma


给定一个二进制,用围棋编译GOOS=linux和GOARCH=amd64 ,部署到docker基于容器alpine:3.3 ,如果码头工人发动机主机的Ubuntu(15.10)二进制将不会运行: sh: /bin/artisan: not found 如果Docker引擎主机是在Mac OS X上的VirtualBox VM中部署的busybox (这是alpine的基地),则此相同二进制文件(针对相同操作系统和arch编译)将运行得很好 。 如果容器基于Ubuntu镜像之一,这个相同的二进制文件也可以很好地运行。 任何

What is the idiomatic Go equivalent of C's ternary operator?

In C/C++ (and many languages of that family), a common idiom to declare and initialize a variable depending on a condition uses the ternary conditional operator : int index = val > 0 ? val : -val Go doesn't have the conditional operator. What is the most idiomatic way to implement the same piece of code as above ? I came to the following solution, but it seems quite verbose var index


在C / C ++(以及该族的许多语言)中,根据条件声明和初始化变量的常用习惯用法使用三元条件运算符: int index = val > 0 ? val : -val Go没有条件运算符。 实现与上面相同的一段代码的最习惯的方式是什么? 我来到以下解决方案,但它看起来很冗长 var index int if val > 0 { index = val } else { index = -val } 有什么更好的吗? 正如所指出的(希望不出所料),使用if+else确实是在Go中使用条件语