Build Go applications using Project IDX and the Gemini API

Build Go applications using Project IDX and the Gemini API

Project IDX, launched in June 2024, is an AI-assisted workspace for full-stack app growth within the cloud. It helps a variety of frameworks, languages and companies, together with integrations for Google merchandise to streamline growth workflows.

We’re going to reap the benefits of this to indicate you the right way to construct purposes with Go in minutes. We’ll arrange a Go growth surroundings in Mission IDX and create your first server utility. Let’s stroll by means of establishing a Go growth surroundings in IDX and making a easy hiya world server.

There are a couple of other ways to create workspaces in IDX. You’ll be able to import a GitHub repository, create a brand new clean workspace from scratch, or use a preconfigured template. We’re going to indicate you the right way to begin from scratch first, after which we’ll check out templates


Getting began

This part will stroll by means of establishing the surroundings and writing a fundamental Good day, World server with IDX.

Let’s get began by creating a brand new clean mission in IDX from idx.google.com/new/blank. This mission comprises a README and a default dev.nix.


Surroundings Customization

Surroundings configuration will be custom-made with nix surroundings configurations. A minimal configuration for a Go workspace in IDX will add the Go nix bundle and set up the Go extension:

Replace .idx/dev.nix to incorporate the Go nix bundle and the Go extension:

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

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

Rebuild your surroundings to permit these modifications to take impact.


Write Go code

Now that the workspace is about up for Go code growth, we are able to begin writing our Go server.

First, let’s initialize the module that can include our Go code. You are able to do this by operating the > Go: Initialize go.mod offered by the Go extension from the Command Palette, or by operating go mod init from the command line.

$ go mod init github.com/myorg/helloWorld

Let’s create a main.go with a easy server that returns “Good day, World!”

bundle primary

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

func primary() {
	log.Print("beginning server...")
	http.HandleFunc("/", handler)

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

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

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

Preview in IDX

Now that we’ve a server that may hearken to requests, let’s try it out by making a preview.

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

idx = {
      previews = {
      allow = true;
      previews = {
          net = {
          command = ["go" "run" "main.go"];
          supervisor = "net";
          env = {
            # Surroundings variables to set to your server
            PORT = "$PORT";
          };
        };
      };
    };
  };

Rebuild the surroundings once more to see the online preview. The preview may also be opened from the Command Palette utilizing > Mission IDX: Present Net Preview.

Discover Go Templates in IDX

To begin rapidly, we’re offering you with prepared to make use of templates that embrace a pre-configured surroundings with all of the instruments and the libraries wanted.

Begin with one of many Go backend server templates or begin constructing LLM purposes with the Go and Gemini template.

Gemini with Go template is built-in with Gemini API to leverage the ability of AI. Plug in your Gemini API key to get going.

Leave a Reply