我正在玩最近去尝试制作一些服务器,它在tcp连接上响应客户端.
我的问题是如何干净地关闭服务器并中断当前在以下调用中"阻止"的go例程
func(*TCPListener)接受?
Accept在Listener接口中实现Accept方法; 它等待下一个调用并返回一个通用的Conn.
这些错误也几乎没有记录.
我一直在Go中使用汇编语言,我写了一个Hamming Weight函数作为练习.
我在这个SO答案上建立了一个原生的Go版本,而汇编版本是基于AMD的这个文档(第180页).在对两个函数进行基准测试后,我发现本机Go版本比汇编版本快1.5倍 - 2倍,尽管手写的汇编版本几乎与输出版本完全相同go tool 6g -S popcount.go.
输出来自 go test -bench=.
PASS
BenchmarkPopCount 100000000 19.4 ns/op
BenchmarkPopCount_g 200000000 8.97 ns/op
ok popcount 4.777s
Run Code Online (Sandbox Code Playgroud)
popcount.go
package popcount
func popCount(i uint32) uint32 // Defined in popcount_amd64.s
func popCount_g(i uint32) uint32 {
i = i - ((i >> 1) & 0x55555555)
i = (i & 0x33333333) + ((i >> 2) & 0x33333333)
return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> …Run Code Online (Sandbox Code Playgroud) 我有一个Windows服务,它使用类库中的方法与相同的asp.net解决方案.在类库中,我有一个方法如下:
reader = XmlReader.Create(HttpContext.Current.Server
.MapPath("~/TestDevice/Data.xml"), settings);
Run Code Online (Sandbox Code Playgroud)
当控制来到这条线.我得到例外.我试图调试代码,发现当服务尝试访问此方法时,HttpContext.Current.Server就是null.什么是替代语法.
我试图从Web应用程序访问此类库方法,它工作正常.
请建议解决方案.
我遇到了一些我为密码认证库编写的Go代码的问题.一般的想法是提供2个函数,Check()和New(),它们都提供了密码和256位HMAC密钥.Check()函数还提供256位salt和256位散列,并返回一个布尔值.New()函数返回一个新的随机salt,它是相应的哈希值.这两个函数都依赖于辅助函数hash(),它使用scrypt进行键延长,并且可以生成输出哈希.
当我最初编写它时,这是有效的(事实证明,我已经使用了早期丢失的代码修订版生成的测试数据).
我现在遇到的问题是,当提供由旧版本代码生成的数据时,Check()函数似乎工作得很好,但现在似乎失败了代码自己的New()函数生成的任何数据(两者都使用底层的hash()函数).
我知道,我应该从一开始就有git版本控制代码!我现在已经吸取了教训.
我已将函数分组并将问题快速演示到一个.go文件中,如下所示,并添加了一些输出用于调试:
package main
import (
"code.google.com/p/go.crypto/scrypt"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
"errors"
"fmt"
"io"
)
// Constants for scrypt. See code.google.com/p/go.crypto/scrypt
const (
KEYLENGTH = 32
N = 16384
R = 8
P = 1
)
// hash takes an HMAC key, a password and a salt (as byte slices)
// scrypt transforms the password and salt, and then HMAC transforms the result.
// Returns the resulting 256 bit hash.
func hash(hmk, pw, s []byte) (h []byte, …Run Code Online (Sandbox Code Playgroud) Can mgo return error different than QueryError or ErrNotFound? What with database connection errors?
Is it a good practise to panic on error different than ErrNotFound and recover on the top of http handlers stack with something like pretty response with status 500?
我正在尝试通过弹性搜索获取搜索结果,但出现此错误:
cannot use true (type bool) as type string in function argument
请任何人可以帮助解决该问题?我是Go编程的新手,所以希望您将来提出改进此情况的建议。
这是我的代码段
func SearchCallback(w *http.ResponseWriter, req *http.Request) {
api.Domain = "127.0.0.1"
// search males
searchQuery := `{
"query": {
"term": {"content":"male"}
}
}`
var response2 map[string]interface{}
response2, err := core.SearchRequest(true, "people", "male", searchQuery, "")
if err != nil {
log.Fatalf("The search of males has failed:", err)
}
var values2 []interface{}
for _, v := range response2.Hits.Hits {
var value2 map[string]interface{}
err := json.Unmarshal(v.Source, &value2)
if err != nil …Run Code Online (Sandbox Code Playgroud)