Benchmarking code with a StopWatch or similar

I'm trying to use Go package "time" to benchmark a function in my program. Been searching around, and people seem to use time.NanoSeconds() - but I think this function is no longer in the time package? What I need is an easy way to start and stop a timer, and print out the value. Use the existing functionality of the testing package inside your *_test.go: func BenchmarkMyFunc(

使用StopWatch或类似工具对代码进行基准测试

我正在尝试使用Go软件包“时间”来对我的程序中的函数进行基准测试。 一直在四处搜索,而人们似乎使用time.NanoSeconds() - 但我认为这个函数已经不在时间包中了? 我需要的是一个简单的方法来启动和停止计时器,并打印出该值。 使用* _test.go中的测试包的现有功能: func BenchmarkMyFunc(b *testing.B) { for i := 0; i < b.N; i++ { myBecnhmarkedFunction() } } 并调用go test -bench RE (RE是

mmonit golang restarting slow and status does not exist

I created monit app that must restart golang site on crash $ cd /etc/monit/conf.d $ vim checkSite It starting program with nohup and saving its pid to file: check process site with pidfile /root/go/path/to/goSite/run.pid start program = "/bin/bash -c 'cd /root/go/path/to/goSitePath; nohup ./goSite > /dev/null 2>&1 & echo $! > run.pid'" with timeout 5 seconds stop pro

mmonit golang重启速度慢,状态不存在

我创建了应用程序,它必须在崩溃时重新启动golang site $ cd /etc/monit/conf.d $ vim checkSite 它使用nohup启动程序并将其pid保存到文件中: check process site with pidfile /root/go/path/to/goSite/run.pid start program = "/bin/bash -c 'cd /root/go/path/to/goSitePath; nohup ./goSite > /dev/null 2>&1 & echo $! > run.pid'" with timeout 5 seconds stop program = "/bin/kill -9 `c

Export structure only for testing in Golang

I have a utility package that many other packages use. I also created some test structures that implement those interfaces. And I put them in interfaces_test.go I'd like to be able to import those test structures in other packages in my *_test.go files. I saw something like that in http://golang.org/src/pkg/os/export_test.go but whatever I try I get an error similar to this one: go tes

导出结构仅用于在Golang中进行测试

我有许多其他软件包使用的实用程序包。 我还创建了一些实现这些接口的测试结构。 我把它们放在interfaces_test.go 我希望能够在我的*_test.go文件中的其他包中导入这些测试结构。 我在http://golang.org/src/pkg/os/export_test.go中看到类似的东西,但是无论我尝试什么,我都会得到与此类似的错误: go test something/mypackage mypackage/ant_lat_lon_test.go:46: undefined: rutl.TestAntenner FAIL something/mypa

Handling read/write udp connection in Go

I need to create UDP connection through which I could write and read packets simultaneously. (using different goroutines and with GOMAXPROCS(n) where n>1) First attempt was something like this: func new_conn(port, chan_buf int) (conn *net.UDPConn, inbound chan Packet, err error) { inbound = make(chan Packet, chan_buf) conn, err = net.ListenUDP("udp4", &net.UDPAddr{Port: port})

在Go中处理读/写udp连接

我需要创建UDP连接,通过它我可以同时写入和读取数据包。 (使用不同的goroutine和GOMAXPROCS(n),其中n> 1)第一次尝试是这样的: func new_conn(port, chan_buf int) (conn *net.UDPConn, inbound chan Packet, err error) { inbound = make(chan Packet, chan_buf) conn, err = net.ListenUDP("udp4", &net.UDPAddr{Port: port}) if err != nil {return} go func () { for {

Golang 1.2: Unzip password protected zip file?

Looking at the latest release (1.2) zip package - how can I unzip a file that was password protected (using 7zip, AES-256 encoding)? I don't see where/how to add in that information. A simple example would be great! The archive/zip package seems to only provide basic zip functionality. I would use 7zip to unzip password protected zip files using the os/exec package. Online 7-zip user g

Golang 1.2:解压缩密码保护的zip文件?

查看最新版本(1.2)zip包 - 如何解压缩受密码保护的文件(使用7zip,AES-256编码)? 我没有看到在那里/如何添加这些信息。 一个简单的例子会很棒! archive/zip包似乎只提供基本的zip功能。 我会使用7zip解压缩使用os/exec软件包的受密码保护的zip文件。 在线7-zip用户指南 了解7zip的最佳指南是7-zip.chm,它位于windows命令行的zip文件中。 下面的代码不是最优的,但它会告诉你如何完成工作。 使用7zip提取受密

go build doesn't find my C standard library when compiling cgo package

I'm trying to compile a go project in a raspberry pi. The project has 5 files, two small .c files and its counterparts .h (one of these files is my code -- it calls the other, which is a base64 library) and a .go files which calls my .c code using cgo . When I compile my C code only (with its calls and everything) with gcc alone at the raspberry pi it does well without any configuration.

编译cgo软件包时,无法找到我的C标准库

我正在尝试编译一个覆盆子pi中的go项目。 该项目有5个文件,两个小型.c文件及其对应文件.h (其中一个文件是我的代码 - 它调用另一个,它是一个base64库)和一个.go文件,它使用cgo调用我的.c代码。 当我只用gcc单独编译我​​的C代码(包括其调用和所有内容)时,它在没有任何配置的情况下运行得很好。 当我使用go build在我的x86 Linux Ubuntu机器上编译整个go项目时,它也相当不错。 但是,当我尝试去编译go项目, go b

Is there a foreach loop in Go?

Is there a foreach construct in the Go language? Can I iterate over a slice or array using a for ? http://golang.org/doc/go_spec.html#For_statements A "for" statement with a "range" clause iterates through all entries of an array, slice, string or map, or values received on a channel. For each entry it assigns iteration values to corresponding iteration variables and the

Go有没有foreach循环?

Go语言中是否有foreach构造? 我可以使用for循环遍历切片或数组吗? http://golang.org/doc/go_spec.html#For_statements 具有“范围”子句的“for”语句遍历数组,切片,字符串或映射的所有条目或通道上接收的值。 对于每个条目,它将迭代值分配给相应的迭代变量,然后执行该块。 举个例子: for index, element := range someSlice { // index is the index where we are // element is the element from someSlic

Does Go have lambda expressions or anything similar?

Does Go support lambda expressions or anything similar? I want to port a library from another language that uses lambda expressions (Ruby). 这里是一个例子,仔细复制和粘贴: package main import fmt "fmt" type Stringy func() string func foo() string{ return "Stringy function" } func takesAFunction(foo Stringy){ fmt.Printf("takesAFunction: %vn", foo()) } func returnsAFunction()Stringy{

Go有lambda表达式或类似的东西吗?

Go支持lambda表达式还是类似的东西? 我想从另一种使用lambda表达式(Ruby)的语言中移植一个库。 这里是一个例子,仔细复制和粘贴: package main import fmt "fmt" type Stringy func() string func foo() string{ return "Stringy function" } func takesAFunction(foo Stringy){ fmt.Printf("takesAFunction: %vn", foo()) } func returnsAFunction()Stringy{ return func()string{ fmt.Printf("Inner stri

Implications of defining a struct inside a function vs outside?

Are there any implications (GC churn, performance, or otherwise) to defining an struct inside a function vs. having it defined outside? For example: type Outside struct { Foo string `json:"foo"` } func SomeFunc(b []byte) error { outside := Outside{} if err := json.NewDecoder(b).Decode(&outside); err != nil { return err } ... } vs. func SomeFunc(b []byte) error { type i

在函数内部和外部定义结构的含义?

是否有任何影响(GC流失,性能或其他)来定义一个struct内部的struct与外部定义? 例如: type Outside struct { Foo string `json:"foo"` } func SomeFunc(b []byte) error { outside := Outside{} if err := json.NewDecoder(b).Decode(&outside); err != nil { return err } ... } 与 func SomeFunc(b []byte) error { type inside struct { Foo string `json:"foo"` } if err := json.N

Verify gpg signature in Go openpgp

I'm playing with writing a Go program that downloads and verifies files. I am hoping to avoid forcing the user to install gnupg (if possible). Is it possible to verify a downloaded file with a gpg signature (asc file) as described here or here using Go's openpgp lib or some other Go library? Any examples demonstrating how to use openpgp to verify a file with an asc signature would be

在Go openpgp中验证gpg签名

我正在玩一个Go程序来下载和验证文件。 我希望避免强制用户安装gnupg(如果可能的话)。 是否有可能使用gpg签名(asc文件)验证下载文件,如此处或此处所述,使用Go的openpgp lib或其他Go库? 我们将感激所有演示如何使用openpgp来验证具有asc签名的文件的例子。 我能够使用以下代码验证gpg签名: package main import ( "fmt" "golang.org/x/crypto/openpgp" "os" ) func main() { ke