Build Go applications using Project IDX and the Gemini API

DEC 18, 2024
Suzy Mueller Software Engineer
Nooras Saba Engineering Manager

Project IDX, launched in June 2024, is an AI-assisted workspace for full-stack app development in the cloud. It supports a wide range of frameworks, languages and services, along with integrations for Google products to streamline development workflows.

We’re going to take advantage of this to show you how to build applications with Go in minutes. We'll set up a Go development environment in Project IDX and create your first server application. Let's walk through setting up a Go development environment in IDX and creating a simple hello world server.

There are a few different ways to create workspaces in IDX. You can import a GitHub repository, create a new blank workspace from scratch, or use a preconfigured template. We’re going to show you how to start from scratch first, and then we’ll take a look at templates


Getting started

This section will walk through setting up the environment and writing a basic Hello, World server with IDX.

Let’s get started by creating a new blank project in IDX from idx.google.com/new/blank. This project contains a README and a default dev.nix.


Environment Customization

Environment configuration can be customized with nix environment configurations. A minimal configuration for a Go workspace in IDX will add the Go nix package and install the Go extension:

Update .idx/dev.nix to include the Go nix package and the Go extension:

{ pkgs, ... }: {
  packages = [
    pkgs.go
  ];

  idx = {
    extensions = [
      "golang.go"
    ];
  };
}

Rebuild your environment to allow these changes to take effect.


Write Go code

Now that the workspace is set up for Go code development, we can start writing our Go server.

First, let's initialize the module that will contain our Go code. You can do this by running the > Go: Initialize go.mod provided by the Go extension from the Command Palette, or by running go mod init from the command line.

$ go mod init github.com/myorg/helloWorld

Let’s create a main.go with a simple server that returns “Hello, World!"

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
)

func main() {
	log.Print("starting server...")
	http.HandleFunc("/", handler)

	// Determine port for HTTP service.
	port := os.Getenv("PORT")
	if port == "" {
		port = "3000"
		log.Printf("defaulting to port %s", port)
	}

	// Start HTTP server.
	log.Printf("listening on port %s", port)
	if err := http.ListenAndServe(":"+port, nil); err != nil {
		log.Fatal(err)
	}
}

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "<!DOCTYPE html>\n")
	fmt.Fprint(w, "Hello, World!\n")
}

Preview in IDX

Now that we have a server that can listen to requests, let’s test it out by creating a preview.

To .idx/dev.nix, add the web preview configuration:

idx = {
      previews = {
      enable = true;
      previews = {
          web = {
          command = ["go" "run" "main.go"];
          manager = "web";
          env = {
            # Environment variables to set for your server
            PORT = "$PORT";
          };
        };
      };
    };
  };

Rebuild the environment again to see the web preview. The preview can also be opened from the Command Palette using > Project IDX: Show Web Preview.

Show Web Preview in Project IDX

Explore Go Templates in IDX

To start quickly, we are providing you with ready to use templates that include a pre-configured environment with all the tools and the libraries needed.

Start with one of the Go backend server templates or start building LLM applications with the Go and Gemini template.

Gemini with Go template is integrated with Gemini API to leverage the power of AI. Plug in your Gemini API key to get going.

Code editor displays project files and a web page titled "Baking with Gemini"  with images of baked goods and a prompt to provide a recipe in IDX