小编Thi*_*Guy的帖子

如何在Go中使用文字*int64?

我有一个带*int64字段的结构类型.

type SomeType struct {
    SomeField *int64
}
Run Code Online (Sandbox Code Playgroud)

在我的代码中的某个时刻,我想声明一个这样的文字(比如,当我知道所述值应为0,或者指向0时,你知道我的意思)

instance := SomeType{
    SomeField: &0,
}
Run Code Online (Sandbox Code Playgroud)

...除了这不起作用

./main.go:xx: cannot use &0 (type *int) as type *int64 in field value
Run Code Online (Sandbox Code Playgroud)

所以我试试这个

instance := SomeType{
    SomeField: &int64(0),
}
Run Code Online (Sandbox Code Playgroud)

......但这也行不通

./main.go:xx: cannot take the address of int64(0)
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?我能想出的唯一解决方案是使用占位符变量

var placeholder int64
placeholder = 0

instance := SomeType{
    SomeField: &placeholder,
}
Run Code Online (Sandbox Code Playgroud)

注意:当它是*int而不是a时,&0语法工作正常*int64.编辑:不,不.为此表示歉意.

编辑:

显然,我的问题含糊不清.我正在寻找一种方法来说明一个*int64.这可以在构造函数中使用,或者用于表示文字结构值,或者甚至作为其他函数的参数.但帮助函数或使用不同的类型不是我正在寻找的解决方案.

struct pointers literals go

79
推荐指数
3
解决办法
4万
查看次数

MongoDB索引复杂度

我真的很喜欢MongoDB,我在工作和家庭中都使用它,但是还没有遇到性能,复杂性或限制性问题。但是我一直在思考索引,但有一个问题我找不到合适的答案。

大规模SQL数据库的主要问题之一是查询的相对复杂性。具体来说,MySQL对大多数索引使用b树,这种查询比线性查询要花O(log(n))更好,但仍然意味着您拥有的数据越多,花费的时间就越长。

NoSQL数据库的一大吸引力在于消除/缓解了这种扩展问题,通常依赖于哈希样式索引,该索引具有O(1)查找时间,因此拥有更多数据不会降低应用程序的运行速度。这是我的问题所在:

根据官方的MongoDB文档,Mongo中的所有索引都使用b树。尽管Mongo实际上确实具有哈希索引,但据我所知,它们仍存储在b树中,与_id字段上的索引相同。在Mongo的文档中,我什至找不到任何能表明恒定时间的东西!

所以我的问题是这样的:实际上,Mongo中的所有索引(包括_id和哈希值)是否都存储在b树中?这是否意味着查询键(甚至通过_id来查询)实际上需要O(log(n))时间?

附录:值得注意的是,如果Mongo文档在示例查询中提供了一些复杂度公式,那将是一件很棒的事情。我最喜欢的示例是Redis文档

另外:这是相关的。但是,我还添加了有关哈希索引和(更重要的是)_id索引的特定问题。

time-complexity mongodb database-performance query-performance

6
推荐指数
1
解决办法
3435
查看次数

无法在mac osx 10.10上构建python gevent

概要: pip install gevent不起作用.

向下挖,我下载了gevent .tar.gz并手动运行构建:python setup.py build,得到了同样的错误:

running build
running build_py
running build_ext
building 'gevent.core' extension
clang -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -U__llvm__ -DLIBEV_EMBED=1 -DEV_COMMON= -DEV_CLEANUP_ENABLE=0 -DEV_EMBED_ENABLE=0 -DEV_PERIODIC_ENABLE=0 -Ibuild/temp.macosx-10.10-x86_64-2.7/libev -Ilibev -I/Users/travisjohnson/.pyenv/versions/2.7.5/include/python2.7 -c gevent/gevent.core.c -o build/temp.macosx-10.10-x86_64-2.7/gevent/gevent.core.o
In file included from gevent/gevent.core.c:313:
In file included from gevent/libev.h:2:
...
29 various compiler warnings
...
29 warnings generated.
clang -bundle -bundle_loader python.exe -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/travisjohnson/.pyenv/versions/2.7.5/lib -U__llvm__ build/temp.macosx-10.10-x86_64-2.7/gevent/gevent.core.o -o build/lib.macosx-10.10-x86_64-2.7/gevent/core.so
ld: file not found: python.exe
clang: error: linker command …
Run Code Online (Sandbox Code Playgroud)

python pip gevent

5
推荐指数
1
解决办法
2387
查看次数

为什么 CPython 退出时没有释放所有内存?

我想说清楚,我没有看到这个问题描述的行为。相反,我的问题是关于问题本身:

蟒蛇3官方FAQ说,这逐字:

为什么 CPython 退出时没有释放所有内存?

并提供了这个答案:

当 Python 退出时,从 Python 模块的全局命名空间引用的对象并不总是被释放。如果存在循环引用,则可能会发生这种情况。还有一些由 C 库分配的内存是不可能释放的(例如,像 Purify 这样的工具会抱怨这些)。然而,Python 在退出时会积极清理内存,并且确实会尝试销毁每个对象。

如果你想强制 Python 在释放时删除某些东西,请使用atexit模块运行一个函数来强制这些删除。

假设一个托管内存操作系统(Linux、Mac、Windows、GNU、BSD、Solaris...),这听起来完全是胡说八道。

在程序退出时(无论是 Python 还是其他任何东西),它从操作系统请求的任何内存都被释放(因为操作系统可以控制虚拟页表等)。该程序不必取消分配或解构任何东西(程序过去必须做的事情,正如有人使用哈希表解构cp遇到瓶颈时所强调的那样),但我认为没有任何操作系统' Python 3 支持对程序提出了这个要求。

这在我不知道的某些情况下有意义吗?这是指什么?

python documentation memory-management python-3.x

5
推荐指数
2
解决办法
2051
查看次数

Terraform:变量的动态属性(以splat语法显示)

在terraform HCL中,是否可以从变量动态引用对象的属性?

即:

variable "attribute" {
  type = "string"
}

data "terraform_remote_state" "thing" {
  not_really_important
}

output "chosen" {
  value = "${data.terraform_remote_state.thing.$var.attribute}"
}
Run Code Online (Sandbox Code Playgroud)

更具体到我的情况,我希望用splat语法做到这一点:

variable "attribute" {
  type = "string"
}

data "terraform_remote_state" "thing" {
  count = 3 # really this is also a variable
  not_really_important
}

output "chosen" {
  value = "${data.terraform_remote_state.thing.*.$var.attribute}"
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了类似的东西lookup(data.terraform_remote_state.thing, var.attribute)(对于splat问题),lookup(element(data.terraform_remote_state.*, count.index), var.attribute)但他们都抱怨我的属性引用不完整/错误的形式.

terraform hcl

5
推荐指数
1
解决办法
522
查看次数