Go: example with struct as activity return type

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 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.

We don’t have a simple example to demonstrate structure as an argument or result of an activity. But Particle Swarm Optimization sample does use structures this way.

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

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)?

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

    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
}
1 Like