Skip to content

Instantly share code, notes, and snippets.

@chiiph
chiiph / defer.go
Created December 26, 2012 20:14
Defer example in Go
func CopyFile(dstName, srcName string) (written int64, err error) {
src, err := os.Open(srcName)
if err != nil {
return
}
defer src.Close()
dst, err := os.Create(dstName)
if err != nil {
return
std::shared_ptr<GameObject> gameObject { std::make_shared<GameObject>("Bomb") };
cocos2d::CCLabelTTF *myLabel = cocos2d::CCLabelTTF::create("Explode!", "Arial", 30.0f);
auto myLambda = [gameObject]()
{
if (gameObject.use_count() > 1)
{
gameObject->explode();
}
});
int nonModifiable = 42;
int modifiable = 23;
auto myLambda = [=, &modifiable]() mutable { modifiable++; nonModifiable++; };
myLambda(); // modifiable == 24, nonModifiable == 42
__block int variable = 1;
int anotherVariable = 2;
void (block^)() = ^{
variable++; // actually modifying variable
anotherVariable++; // modifying a copy
}