You're taking the next step in your journey towards building better apps!
This tutorial is for developers who are relatively new to Temporal and have some basic knowledge of Go. We recommend setting aside ~20 minutes to complete. By following this tutorial you will achieve a few things:
- Learn how to set up a Temporal Go application project.
- Become more familiar with core concepts and the application structure.
- Build and test a simple "Hello World!" Temporal Workflow application from scratch using the Temporal Go SDK.
This tutorial focuses on the practicalities of building an application from scratch. To better understand why you should use Temporal, we recommend that you follow the tutorial where you run a Temporal money transfer application to get a taste of its value propositions.
All of the code in this tutorial is available in the hello-world Go template repository.
Scaffold Go project
Before starting, make sure you have looked over the tutorial prerequisites.
In a terminal, create a new project directory named "hello-world-project", or something similar and cd into it.
From the root of your new project directory, initialize a new Go module:
Then, add the Temporal Go SDK as a project dependency:
"Hello World!" app
Now we are ready to build our Temporal Workflow application. Our app will consist of four pieces:
- An Activity: An Activity is just a function that contains your business logic. Ours will simply format some text and return it.
- A Workflow: Workflows are functions that organize Activity method calls. Our Workflow will orchestrate the call of a single Activity function.
- A Worker: Workers host the Activity and Workflow code and execute the code piece by piece.
- An initiator: To start a Workflow, we must send a signal to the Temporal server to tell it to track the state of the Workflow. We'll write a separate function to do this.
First, let's define our Activity function which is just like any other function in Go. Activities are meant to handle non-deterministic code that could result in an error. But for this tutorial all we are doing is taking a string, appending it to "Hello", and returning it back to the Workflow.
Create activity.go and add the following code:
Next is our Workflow. Workflow functions are where you configure and organize the execution of Activity functions. Again, the Workflow function is defined like any other Go function. Our Workflow just calls
ComposeGreeting() and returns the result.
Create workflow.go and add the following code:
Task Queues are how the Temporal server supplies information to Workers. When you start a Workflow, you tell the server which Task Queue the Workflow and/or Activities use as an information queue. We will configure our Worker to listen to the same Task Queue that our Workflow and Activities use. Since the Task Queue name is used by multiple things, let's create shared.go and define our Task Queue name there:
Our Worker hosts Workflow and Activity functions and executes them one at a time. The Worker is instructed to execute a specific function via information it pulls from the Task Queue. After it runs the code, it communicates the results back to the server.
Create worker/main.go and add the following code:
There are two ways to start a Workflow, via the Temporal CLI or Temporal SDK. In this tutorial we use the SDK to start the Workflow which is how most Workflows are started in live environments.
Create start/main.go and add the following code:
Test the app
Let's add a simple unit test to our application to make sure things are working as expected. Create workflow_test.go and add the following code:
Run this command from the project root to execute the unit tests:
Run the app
To run the app we need to start the Workflow and the Worker. You can start them in any order, but run each command from a separate terminal window.
To start the Worker run this command from the project root:
To start the Workflow run this command from the project root:
Congratulations you have successfully built a Temporal application from scratch!
Great work! You now know how to build a Temporal Workflow application using the Go SDK. Let's do a quick review to make sure you remember some fo the more important pieces.
What are the minimum four pieces of a Temporal Workflow application?
- An Activity function.
- A Workflow function.
- A Worker to host the Activity and Workflow code.
- A frontend to start the Workflow.
How does the Temporal server get information to the Worker?
It adds the information to a Task Queue.
True or false, Temporal Activity and Workflow functions are just normal Go functions?