我想创建一个GatewayInfo类型定义的JSON,如下所示:
type SpanInfo struct {
imsi string
network string
network_status string
signal_quality int
slot int
state string
}
type GatewayInfo []SpanInfo
Run Code Online (Sandbox Code Playgroud)
网关信息创建于:
var gatewayInfo = make(GatewayInfo, nb_spans)
Run Code Online (Sandbox Code Playgroud)
要创建JSON,我使用以下json.Marshal函数:
gatewayInfo := getGatewayInfo(spans)
log.Printf("Polling content: %s\n", gatewayInfo)
jsonInfo, _ := json.Marshal(gatewayInfo)
log.Printf("jsonInfo: %s\n", jsonInfo)
Run Code Online (Sandbox Code Playgroud)
不幸的是结果不是我所期待的:
2015/02/09 13:48:26 Polling content: [{652020105829193 20801 Registered (Roaming) %!s(int=17) %!s(int=2) } {652020105829194 20801 Registered (Roaming) %!s(int=16) %!s(int=3) } {652020105829192 20801 Registered (Roaming) %!s(int=19) %!s(int=1) } {652020105829197 20801 Registered (Roaming) %!s(int=19) %!s(int=4) }]
2015/02/09 …Run Code Online (Sandbox Code Playgroud) 我想知道为什么不可能在go中执行以下操作:
func main() {
stuff := []string{"baz", "bla"}
foo("bar", stuff...)
}
func foo(s ...string) {
fmt.Println(s)
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,切片......"爆炸"切片,以便它可以用于多参数函数调用.所以上面的例子实际上应该扩展到foo("bar", "baz", "bla").
foo(stuff...) 按预期工作,这里没有惊喜,但在上面的例子中,编译器抱怨过多的参数.
这是一个理想的限制吗?我来自一个foo("bar", *stuff)非常好的红宝石背景(并且,至少在我的书中,同样的事情),这就是为什么这让我感到惊讶.
当我跑go test,我的输出:
--- FAIL: TestGETSearchSuccess (0.00s)
Location: drivers_api_test.go:283
Error: Not equal: 200 (expected)
!= 204 (actual)
--- FAIL: TestGETCOSearchSuccess (0.00s)
Location: drivers_api_test.go:391
Error: Not equal: 200 (expected)
!= 204 (actual)
Run Code Online (Sandbox Code Playgroud)
但是在我go test再次运行之后,我的所有测试都通过了.
只有当我重置我的mysql数据库,然后go test第一次运行时,测试才会失败.
对于每个GET请求,我POST之前都会请求确保在数据库中创建数据.
谁能帮助我如何确保测试按顺序运行?那是POST请求在GET请求之前运行?
为什么Go最终会采用恐慌/恢复的异常处理,当语言如此惯用并且是错误代码的强烈提倡者时?Go的设计者有哪些场景不会被错误代码处理并且需要恐慌/恢复?
我理解约定说限制恐慌/恢复,但运行时是否也限制它们不能用作C++中的常规throw/catch?
我有一个多线程的应用程序,工作正常.然而,它正在达到锁争用问题(通过快照java堆栈并查看最新等待来检查).
每个线程都会从列表中删除对象,并拒绝每个对象或将其放入Bin中.
Bins最初为null,因为每个都很昂贵(并且可能有很多).
导致争用的代码看起来大致如下:
public void addToBin(Bin[] bins, Item item) {
Bin bin;
int bin_index = item.bin_index
synchronized(bins) {
bin = bins[bin_index];
if(bin==null) {
bin = new Bin();
bins[bin_index] = bin;
}
}
synchronized(bin) {
bin.add(item);
}
}
Run Code Online (Sandbox Code Playgroud)
bins数组上的同步是瓶颈.
一位同事建议我使用双重检查锁定来解决这个问题,但我们不确定究竟会涉及到什么使其安全.建议的解决方案如下所示:
public void addToBin(Bin[] bins, Item item) {
int bin_index = item.bin_index
Bin bin = bins[bin_index];
if(bin==null) {
synchronized(bins) {
bin = bins[bin_index];
if(bin==null) {
bin = new Bin();
bins[bin_index] = bin;
}
}
}
synchronized(bin) {
bin.add(item); …Run Code Online (Sandbox Code Playgroud) 假设我有一个类似的结构
type A struct{
name string`json:"name"`
}
Run Code Online (Sandbox Code Playgroud)
然后在主要我有代码
var jsonString string = `{"status":false}`
var a A
error := json.Unmarshal([]byte(jsonString),&a)
Run Code Online (Sandbox Code Playgroud)
显然上面的代码产生了一个nil错误,无论json格式是不同的.什么时候json.Unmarshal()会在Go中返回错误?
在我的代码中,我有这样的基准:
const STR = "abcd"
const PREFIX = "ab"
var STR_B = []byte(STR)
var PREFIX_B = []byte(PREFIX)
func BenchmarkStrHasPrefix(b *testing.B) {
for i := 0; i < b.N; i++ {
strings.HasPrefix(STR, PREFIX)
}
}
func BenchmarkBytHasPrefix(b *testing.B) {
for i := 0; i < b.N; i++ {
bytes.HasPrefix(STR_B, PREFIX_B)
}
}
Run Code Online (Sandbox Code Playgroud)
我对结果有点困惑:
BenchmarkStrHasPrefix-4 300000000 4.67 ns/op
BenchmarkBytHasPrefix-4 200000000 8.05 ns/op
Run Code Online (Sandbox Code Playgroud)
为什么差异高达2倍?
谢谢.
假设我有以下 struct wherevalid用于验证每个验证器(特别是govalidator)的自定义消息的结构。
type Login struct {
Email string `json:"email" valid:"required~Email is required,email~The email address provided is not valid"`
Password string `json:"password" valid:"required~Password is required,stringlength(6|40)~Password length must be between 6 and 40"`
}
Run Code Online (Sandbox Code Playgroud)
添加了一些验证器后,行太长且无法维护。
我想拆分成新行,但不支持 go并且与reflect.StructTag.Get不兼容。
但是,根据我的测试,验证器可以使用多行结构标记,但 vet 失败。
简而言之,拆分长结构标签的正确方法是什么?
我正在与https://github.com/mongodb/mongo-go-driver 合作,目前正在尝试实现此类结构的部分更新
type NoteUpdate struct {
ID string `json:"id,omitempty" bson:"_id,omitempty"`
Title string `json:"title" bson:"title,omitempty"`
Content string `json:"content" bson:"content,omitempty"`
ChangedAt int64 `json:"changed_at" bson:"changed_at"`
}
Run Code Online (Sandbox Code Playgroud)
例如,如果我有
noteUpdate := NoteUpdate{ Title: "New Title" }
Run Code Online (Sandbox Code Playgroud)
然后我希望存储文档中唯一的“标题”字段将被更改。
我需要写一些类似的东西
collection.FindOneAndUpdate(context.Background(),
bson.NewDocument(bson.EC.String("_id", noteUpdate.ID)),
// I need to encode non-empty fields here
bson.NewDocument(bson.EC.SubDocument("$set", bson.NewDocument(...)))
)
Run Code Online (Sandbox Code Playgroud)
问题是我不想用bson.EC.String(...)or手动编码每个非空字段bson.EC.Int64(...)。我尝试使用bson.EC.InterfaceErr(...)但出现错误
无法为 *models.NoteUpdate 类型创建元素,请尝试使用 bsoncodec.ConstructElementErr
不幸的是,bsoncodec 中没有这样的功能。我发现的唯一方法是创建包装器
type SetWrapper struct {
Set interface{} `bson:"$set,omitempty"`
}
Run Code Online (Sandbox Code Playgroud)
并使用它
partialUpdate := &NoteUpdate{
ID: "some-note-id",
Title: "Some new title", …Run Code Online (Sandbox Code Playgroud) 这个特定问题与将 mongodb 与 golang 包一起使用有关mongo-driver,但我认为这适用于大多数与 mongodb 的接口。
当Find用于从集合中查询某些数据时,我们可以同时使用bson.M- 和bson.D- 类型来指定此查找的过滤器。
bson.D如果元素的顺序很重要,bson.M则应根据文档使用,否则应使用。
D 是 BSON 文档的有序表示。当元素的顺序很重要时,应该使用这种类型,例如 MongoDB 命令文档。如果元素的顺序无关紧要,则应使用 M 代替。
现在我的问题是使用这些结构中的任何一个,即有序与无序,结构是否会对 mongo 查询优化器生成的查询计划产生影响。
在经典的 SQL 数据库中,顺序通常无关紧要,因为优化器足够聪明,可以使用汇总统计信息、索引等来确定首先执行哪些查询。
我是否可以假设这里也是这种情况,或者使用有序结构来查询我的集合是否会以某种方式干扰这一点/是否使用类似于使用优化器提示的有序结构工作?如果有一些干扰,这是否受到要搜索的字段是否被索引的影响?