GoLand 2024.3 Help

Using AI Assistant to build an application

This tutorial demonstrates how to use the AI Assistant in GoLand to build a web application from scratch. It covers project creation, generating an application with AI-driven suggestions, running and testing the application, modifying and enhancing its functionality, and finally managing it with version control.

AI Assistant in GoLand might improve the efficiency of routine tasks and make complex challenges more manageable.

Using AI Assistant

Step 1. Create an empty project

During this step, we are going to create a new project and ensure that the necessary Go SDK configuration is set up.

General procedure of creating a project

  1. Click File | New | Project. Alternatively, on the Welcome screen, click New Project.

  2. On the New Project page, select the type of project you want to create. You can refer to the available options in the Project types table.

  3. In the GOROOT field, specify the location of your Go installation. GoLand typically detects this location automatically.

    To change or install a new version of the Go SDK, click Add SDK (Add SDK icon) and select one of the following options:

    • Local: to select a Go SDK version on your local drive.

    • Download: to download the Go SDK from the official repository.

  4. (Optional) To prevent GoLand from creating a main.go file with sample code, clear the Add sample code checkbox. When selected, GoLand creates this file to demonstrate basic IDE functionality.

  5. Click Create.

General procedure of creating a project

With a new project set up, we can proceed to generate an application using the AI Assistant.

Step 2. Generating an application

Next, we are going to generate a web application using the AI Assistant, focusing on code creation for backend and web interfaces.

  1. From the main menu, select View | Tool Windows | AI Assistant.

  2. In the input field, select a model that you want to use. For example, anthropic-claude-3.5-sonnet.

    select a model
  3. In the input field, write the following prompt and press Enter:

    Generate a web application with Go and JavaScript that lists to-do items in the browser. The `createTodoHandler` should handle POST requests for creating a new to-do item. The `ToDoItem` struct should have an ID, Title, and Description, using only standard Go packages.
  4. Wait for the generation to complete.

    The model should generate code for the backend and a web form.

  5. Click the Create File from Snippet icon in the snippet frame.

    Create two files: one for the web form and one for the backend, as instructed by the model.

    Generated code in the editor tab
  6. (Optional) Follow naming for generated files. In this tutorial, names should be main.go and index.html. So, we need to rename ToDoListApp.html to index.html.

    To rename the file, press Shift+F6 or right-click a file and select Refactor | Rename.

    follow naming conventions

We have now generated the initial application structure. Let us ensure it works by running it.

Run the application

  • Click in the gutter near the main function and select Run.

    Run the generated application

After you ensured that everything is working, you can stop it by pressing Ctrl+F2 or the Stop button on the toolbar.

Step 3. Updating the handler

Let's enhance the application by adding new fields to existing structures and updating associated components using the AI Assistant.

First, we will update the application by adding due dates to our to-do items. This modification does not require navigating to the AI Assistant tool window.

Adding a field to the struct

  1. Find the ToDoItem struct in main.go.

  2. Select the whole struct and press Ctrl+\.

  3. In the input field, write the following prompt and press Enter:

    Update the ToDoItem struct to include a DueDate field.
using the ai assistant popup

Second, to reflect this change in the web form, let us proceed with updating JavaScript in the HTML form.

Updating the web form

  1. Double-click index.html to open it in the editor.

  2. From the main menu, select View | Tool Windows | AI Assistant.

  3. In the input field, select a model that you want to use. For example, anthropic-claude-3.5-sonnet.

    select a model
  4. To add the context to the prompt form, click the plus icon and select two files: main.go and index.html.

    context to the prompt form
  5. In the input field, write the following prompt and press Enter:

    Update the web form to include a `DueDate` field. Output the updated HTML file and keep all code.
  6. Wait for code generation, then select all code in index.html and click the Insert Snippet at Caret icon in the generated code frame in the AI Assistant tool window.

    Insert Snippet at Caret

Run the modified application

  • Click in the gutter near the main function and select Run.

    Run the generated application

Once again, let us run the modified application to verify that the new field has been correctly added.

After you ensured that everything is working, you can stop it by pressing Ctrl+F2 or the Stop button on the toolbar.

This time the form should include the Due field. You can try and add some information into the fields and press the button if it was generated.

Step 4. Generating tests

The AI Assistant can help you create tests for your code.

  1. Open main.go in the editor, right-click anywhere in the code, and navigate to AI Actions | Generate Unit Tests.

    Generate Unit Tests
  2. Wait for the generation to complete.

    GoLand will open a window with generated test code.

  3. If the generated code is satisfactory, click Accept all.

    Generating tests

Run generated tests

  • Click in the gutter near the main function and select Run.

    Run the generated application

If you do not understand a code section, use the Explain code action for detailed explanations.

Explaining code

  1. Select code that you want to explain.

  2. Right-click the selection and navigate to AI Actions | Explain code.

    Explaining code

Step 5. Running the application from the terminal

This step covers using the terminal to run your application with custom settings.

And if you do not remember the necessary parameters or commands, you can ask the AI Assistant to generate them.

Enable functionality of a new terminal

  1. Select View | Tool Windows | Terminal from the main menu.

  2. In the Terminal tool window, click the Options icon (Options) and select Enable New Terminal.

    Enable New Terminal

Generating a terminal command

  1. Select View | Tool Windows | Terminal from the main menu.

  2. In the Terminal tool window, click the Ask AI Assistant icon (Ask AI Assistant).

  3. In the input field, write the following prompt and press Enter:

    Run this Go application on port 8080 with race condition detection enabled.
  4. Wait for the generation to complete.

    GoLand will generate a command like go run -race main.go -port 8080.

  5. Press Enter to run the command.

After you ensured that everything is working, you can stop it by pressing Ctrl+F2 or the Stop button on the toolbar.

Step 6. Push your changes to the VCS

Finally, with our application ready, we can generate commit messages and push the changes to our version control system.

Generate a commit message

  1. Select View | Tool Windows | Commit from the main menu.

  2. Click the Generate Commit Message with AI Assistant icon (Generate Commit Message with AI Assistant).

    Generate a commit message
  3. Wait for the generation to complete.

Commit and push your changes

  • In the Commit tool window, click Commit and Push.

Last modified: 19 November 2024