Project IDX, launched in June 2024, is an AI-assisted workspace for full-stack app improvement within the cloud. It helps a variety of frameworks, languages and companies, together with integrations for Google merchandise to streamline improvement workflows.
We’re going to reap the benefits of this to point out you the right way to construct functions with Go in minutes. We’ll arrange a Go improvement setting in Venture IDX and create your first server utility. Let’s stroll by establishing a Go improvement setting in IDX and making a easy whats up world server.
There are just a few other ways to create workspaces in IDX. You possibly can import a GitHub repository, create a brand new clean workspace from scratch, or use a preconfigured template. We’re going to point out you the right way to begin from scratch first, after which we’ll check out templates
Getting began
This part will stroll by establishing the setting and writing a fundamental Howdy, World server with IDX.
Let’s get began by creating a brand new clean venture in IDX from idx.google.com/new/blank. This venture comprises a README and a default dev.nix.
Atmosphere Customization
Atmosphere configuration might be custom-made with nix setting 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 setting to permit these modifications to take impact.
Write Go code
Now that the workspace is about up for Go code improvement, we will begin writing our Go server.
First, let’s initialize the module that may comprise our Go code. You are able to do this by working the > Go: Initialize go.mod
offered by the Go extension from the Command Palette, or by working 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 “Howdy, World!”
bundle foremost
import (
"fmt"
"log"
"internet/http"
"os"
)
func foremost() {
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, "Howdy, World!n")
}
Preview in IDX
Now that we now have a server that may take heed to requests, let’s check it out by making a preview.
To .idx/dev.nix, add the online preview configuration:
idx = {
previews = {
allow = true;
previews = {
internet = {
command = ["go" "run" "main.go"];
supervisor = "internet";
env = {
# Atmosphere variables to set to your server
PORT = "$PORT";
};
};
};
};
};
Rebuild the setting once more to see the online preview. The preview can be opened from the Command Palette utilizing > Venture IDX: Present Internet Preview
.
Discover Go Templates in IDX
To start out shortly, we’re offering you with prepared to make use of templates that embody a pre-configured setting with all of the instruments and the libraries wanted.
Begin with one of many Go backend server templates or begin constructing LLM functions with the Go and Gemini template.
Gemini with Go template is built-in with Gemini API to leverage the facility of AI. Plug in your Gemini API key to get going.