Developing with Go
Step 0. Creating the initial source code (optional)
We will create the example source code by using some popular frameworks.
Before we begin, we will create a new directory and cd into it.
mkdir quickstart-demo && cd quickstart-demo
This is optional and you may use an existing project instead (make sure you cd into the project directory before running any odo commands) or a starter project from odo init
.
For Go, we will create our own application using the standard library:
- Create the following
main.go
file:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", HelloServer)
http.ListenAndServe("0.0.0.0:8080", nil)
}
func HelloServer(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
}
- Initialize a
go.mod
file:
go mod init my.example.go.project
Example
Your source code has now been generated and created in the directory.
Step 1. Preparing the target platform
- Podman
- Kubernetes
- OpenShift
Before starting on Podman, you should have Podman installed and configured properly on your machine. See Podman installation instructions for further details.
You also need odo
3.8.0 or later.
To make sure that odo
has the appropriate version and detects your local Podman,
run the command below and check that it reports the Podman Client version.
odo version
Sample Output
Before starting, you should make sure that odo is connected to your cluster and that you have created a new namespace.
Creating a new namespace
If you are using Kubernetes, you can create a new namespace with the odo create namespace
command.
odo create namespace odo-dev
Sample Output
Before starting, you should make sure that odo is connected to your cluster and that you have created a new project.
Login to OpenShift Cluster
The easiest way to connect odo
to an OpenShift cluster is use copy "Copy login command" function in OpenShift Web Console.
- Login to OpenShift Web Console.
- At the top right corner click on your username and then on "Copy login command".
- You will be prompted to enter your login credentials again.
- After login, open "Display Token" link.
- Copy whole
oc login --token ...
command and paste it into the terminal, before executing the command replaceoc
withodo
.
Create a new project
If you are using OpenShift, you can create a new namespace with the odo create project
command.
odo create project odo-dev
Sample Output
Step 2. Initializing your application (odo init
)
Now we'll initialize your application by creating a devfile.yaml
to be deployed.
odo
handles this automatically with the odo init
command by autodetecting your source code and downloading the appropriate Devfile.
Note: If you skipped Step 0, select a "starter project" when running odo init
.
Let's run odo init
and select Go:
odo init
Sample Output
Step 3. Developing your application continuously (odo dev
)
Now that we've generated our code as well as our Devfile, let's start on development.
odo
uses inner loop development and allows you to code,
build, run and test the application in a continuous workflow.
Once you run odo dev
, you can freely edit code in your favourite IDE and watch as odo
rebuilds and redeploys it.
Let's run odo dev
to start development on your Go application:
- Podman
- Kubernetes
- OpenShift
odo dev --platform podman
Sample Output
odo dev
Sample Output
odo dev
Sample Output
Then wait a few seconds until odo dev
displays Forwarding from 127.0.0.1:...
in its output,
meaning that odo
has successfully set up port forwarding to reach the application running in the container.
You can now access the application via the local port displayed by odo dev
(127.0.0.1:20001 in the sample output above) and start your development loop.
odo
will watch for changes and push the code for real-time updates.
You can now follow the advanced guide to deploy the application to production.