# Go: \`defer\` executing a last activity?

**URL:** <https://community.temporal.io/t/go-defer-executing-a-last-activity/74>\
**Category:** Community Support\
**Tags:** go-sdk\
**Created:** [July 7, 2020, 4:02pm UTC](https://community.temporal.io/t/go-defer-executing-a-last-activity/74 "2020-07-07T16:02:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ringods](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/ringods/32/21_2.png) [@ringods](https://community.temporal.io/u/ringods)\
**Post date:** [July 7, 2020, 4:02pm UTC](https://community.temporal.io/t/go-defer-executing-a-last-activity/74/1 "2020-07-07T16:02:13Z")

</div>

In the documentation of the Go SDK, I see the `defer` used to complete a session. But when not using sessions, can I also use `defer` to execute a last workflow activity.

Conceptually, I want to execute a cleanup activity no matter the outcome of the previous activities.

e.g.

```auto
defer workflow.ExecuteActivity(ctx, activities.Cleanup, tenant, application)

```

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [July 7, 2020, 4:06pm UTC](https://community.temporal.io/t/go-defer-executing-a-last-activity/74/2 "2020-07-07T16:06:33Z")

</div>

Yes, executing any workflow code including activities is supported in the defer.

You should be careful about context passed to `ExecuteActivity` as it can be already cancelled. So I recommend using a disconnected context created through [workflow.NewDisconnectedContext](https://github.com/temporalio/temporal-go-sdk/blob/b085d9912d4a580a654ae8bfcd89a515e0ebcd7c/workflow/context.go#L79) in the cleanup code.

---

<div class="post-metadata">

**Author:** ![ringods](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/ringods/32/21_2.png) [@ringods](https://community.temporal.io/u/ringods)\
**Post date:** [July 7, 2020, 4:24pm UTC](https://community.temporal.io/t/go-defer-executing-a-last-activity/74/3 "2020-07-07T16:24:14Z")

</div>

@maxim I moved the following line close to the top of my workflow implementation and added the new context:

```auto
cleanupCtx, _ := workflow.NewDisconnectedContext(ctx)
defer workflow.ExecuteActivity(cleanupCtx, activities.Cleanup, tenant, application).Get(cleanupCtx, &result)

```

Should the `Get(cleanupCtx, &result)` be appended? With this function chaining, I’m not sure what I am deferring.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [July 7, 2020, 4:58pm UTC](https://community.temporal.io/t/go-defer-executing-a-last-activity/74/4 "2020-07-07T16:58:58Z")

</div>

`defer` is tricky and I wouldn’t rely on it to execute the code you posted correctly. I would change it to:

```auto
defer func() { 
   workflow.ExecuteActivity(cleanupCtx, activities.Cleanup, tenant, application).Get(cleanupCtx, &result)
}()

```

to be on the safe side.
