# Go: example with struct as activity return type

**URL:** <https://community.temporal.io/t/go-example-with-struct-as-activity-return-type/108>\
**Category:** Community Support\
**Tags:** go-sdk\
**Created:** [July 8, 2020, 2:49pm UTC](https://community.temporal.io/t/go-example-with-struct-as-activity-return-type/108 "2020-07-08T14:49:28Z")\
**Posts on this page:** 7\
**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 8, 2020, 2:49pm UTC](https://community.temporal.io/t/go-example-with-struct-as-activity-return-type/108/1 "2020-07-08T14:49:28Z")

</div>

The Go SDK documentation on Activities contains this sentence:

> The result value can be either a basic type or a struct with the only requirement being that it is serializable.

Would it be possible to add a code sample to [temporal-go-examples](https://github.com/temporalio/temporal-go-samples/) showing how to use a struct as the argument type to an activity and a return type from an activity?

Should one use the struct by value or by reference (pointer)? I might be talking rubbish here as I don’t have a lot of Go experience, which makes it all the more important to add such a code example.

---

<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 8, 2020, 3:27pm UTC](https://community.temporal.io/t/go-example-with-struct-as-activity-return-type/108/2 "2020-07-08T15:27:43Z")

</div>

We don’t have a simple example to demonstrate structure as an argument or result of an activity. But [Particle Swarm Optimization](https://github.com/temporalio/temporal-go-samples/tree/master/pso) sample [does use structures this way](https://github.com/temporalio/temporal-go-samples/blob/ac8ef9813074c1a8a76203458b9a67dc4ff43433/pso/swarm.go#L38-L37).

---

<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 8, 2020, 6:23pm UTC](https://community.temporal.io/t/go-example-with-struct-as-activity-return-type/108/3 "2020-07-08T18:23:12Z")

</div>

The function signature of the activity has the `Particle` struct type as the return type rather than a pointer to the struct:

> <https://github.com/temporalio/temporal-go-samples/blob/ac8ef9813074c1a8a76203458b9a67dc4ff43433/pso/activities.go#L29>

This means I can’t write `return nil, err` in case there is an intermediate `error` to return. Can I also have it typed with a `(*Particle, error)`?

---

<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 8, 2020, 6:31pm UTC](https://community.temporal.io/t/go-example-with-struct-as-activity-return-type/108/4 "2020-07-08T18:31:35Z")

</div>

I changed the activity code to the following and it worked:

```auto
    err := workflow.ExecuteActivity(ctx, InitParticleActivityName, &swarm).Get(ctx, &particle)

func InitParticleActivity(ctx context.Context, swarm *Swarm) (*Particle, error) {
	logger := activity.GetLogger(ctx)
	logger.Info("initParticleActivity started.")

	particle := NewParticle(swarm, rng)
	particle.UpdateFitness(swarm)

	return particle, nil
}

```

---

<div class="post-metadata">

**Author:** ![mvp](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/mvp/32/941_2.png) [@mvp](https://community.temporal.io/u/mvp)\
**Post date:** [March 24, 2022, 4:00pm UTC](https://community.temporal.io/t/go-example-with-struct-as-activity-return-type/108/5 "2022-03-24T16:00:17Z")

</div>

Re-asking part of the original question. Sorry if it has somehow been answered already.

> This means I can’t write `return nil, err` in case there is an intermediate `error` to return

Your example above shows returning a pointer to a struct from an activity. But, in the workflow, how do I get nil or the struct ?

For example, the following code doesn’t work in the workflow.

```auto
var particle *Particle
err := workflow.ExecuteActivity(ctx, InitParticleActivityName, &swarm).Get(ctx, particle)
if particle == nil {
  // do something
} else {
  // do something else
}

```

The following works, but I can’t check whether the return value was nil.

```auto
var particle Particle
err := workflow.ExecuteActivity(ctx, InitParticleActivityName, &swarm).Get(ctx, &particle)

```

---

<div class="post-metadata">

**Author:** ![Chad\_Retz](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/chad_retz/32/1396_2.png) [@Chad\_Retz](https://community.temporal.io/u/Chad_Retz)\
**Post date:** [March 24, 2022, 4:15pm UTC](https://community.temporal.io/t/go-example-with-struct-as-activity-return-type/108/6 "2022-03-24T16:15:21Z")

</div>

> [@mvp](#):
>
> For example, the following code doesn’t work in the workflow.

For anything you want to decode into a value, you can pass a pointer for it to see if it was nil. So if you return `*Particle` from the activity, you can pass a `**Particle` into the `Get` call. Try changing `.Get(ctx, particle)` to `.Get(ctx, &particle)` in your first set of code there.

---

<div class="post-metadata">

**Author:** ![mvp](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/mvp/32/941_2.png) [@mvp](https://community.temporal.io/u/mvp)\
**Post date:** [March 24, 2022, 4:28pm UTC](https://community.temporal.io/t/go-example-with-struct-as-activity-return-type/108/7 "2022-03-24T16:28:35Z")

</div>

Hi @Chad_Retz, that works. Thank you so much.
