我正在学习go,我的一些代码看起来像这样:
a, err := doA()
if err != nil {
return nil, err
}
b, err := doB(a)
if err != nil {
return nil, err
}
c, err := doC(b)
if err != nil {
return nil, err
}
... and so on ...
Run Code Online (Sandbox Code Playgroud)
这看起来有点不对,因为错误检查占用了大部分行.有没有更好的方法来进行错误处理?我可以通过一些重构来避免这种情况吗?
更新:谢谢你的所有答案.请注意,在我的示例中,doB取决于a,doC取决于b等.因此,大多数建议的重构在这种情况下不起作用.还有其他建议吗?
以下问题:
我需要类似空范围的东西.这意味着此范围是emtpy,但响应范围通常响应的所有方法.我目前正在使用一些肮脏的黑客.我只是提供"1 = 0"作为条件.我觉得这很难看,因为它击中了数据库.简单地返回一个空数组是行不通的,因为结果必须响应作用域方法.
有没有更好的现有解决方案或我需要自己编码?
也许一些示例代码可以帮助解释我需要的东西:
class User < ActiveRecord::Base
named_scope :admins, :conditions => {:admin => true }
named_scope :none_dirty, :conditions => "1=0" # this scope is always empty
def none_broken
[]
end
def self.sum_score # okay, a bit simple, but a method like this should work!
total = 0
self.all.each do |user|
total += user.score
end
return total
end
end
User.admin.sum_score # the score i want to know
User.none_drity.sum_score # works, but hits the db
User.none_broken.sum_score # ...error, since …
Run Code Online (Sandbox Code Playgroud) 我有一个奇怪的问题malloc
。在使用多个 malloc 分配 <10mb 后,malloc 突然返回地址0x100000000
,这会在访问时导致 SIGSEGV。我不知道出了什么问题。设置errno
为0
并且我的内存有足够的空间,所以这不应该是空间问题。malloc 返回的最后地址小于0x6255f0
。知道要寻找什么吗?
有关我的系统的一些信息:
PMAP 输出:
Address Kbytes RSS Dirty Mode Mapping
0000000000400000 0 32 0 r-x-- tests
000000000060a000 0 4 4 r---- tests
000000000060b000 0 4 4 rw--- tests
000000000060c000 0 116 116 rw--- [ anon ]
00007ffff75cd000 0 348 0 r-x-- libc-2.12.1.so
00007ffff7747000 0 …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 nix flakes 进行 android 开发。
这是我的 flake.nix:
{
description = "test";
outputs = { self, nixpkgs }: {
packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello;
devShell.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.mkShell {
buildInputs = [
nixpkgs.legacyPackages.x86_64-linux.androidenv.androidPkgs_9_0.androidsdk
];
};
};
}
Run Code Online (Sandbox Code Playgroud)
跑步nix develop
给了我这样的信息:
You must accept the following licenses:
- android-sdk-license
by setting nixpkgs config option 'android_sdk.accept_license = true;'.
Run Code Online (Sandbox Code Playgroud)
使用薄片时如何传递此选项nixpkgs
?