Using AI Assistant to build an application
note
The AI Assistant may produce outputs that differ from the examples in this tutorial. This variability is normal, as responses depend on several factors, leading to alternative code snippets, explanations, and suggestions. These differences are expected and often provide valid alternative solutions.
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 Using AI Assistant](https://resources.jetbrains.com/help/img/idea/2024.3/go_using_ai_assistant.png)
During this step, we are going to create a new project and ensure that the necessary Go SDK configuration is set up.
Click File | New | Project. Alternatively, on the Welcome screen, click New Project.
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.
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 (
) 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.
(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.Click Create.
![General procedure of creating a project General procedure of creating a project](https://resources.jetbrains.com/help/img/idea/2024.3/go_general_procedure_of_creating_a_project.png)
With a new project set up, we can proceed to generate an application using the AI Assistant.
Next, we are going to generate a web application using the AI Assistant, focusing on code creation for backend and web interfaces.
From the main menu, select View | Tool Windows | AI Assistant.
In the input field, select a model that you want to use. For example,
anthropic-claude-3.5-sonnet
.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.
Wait for the generation to complete.
The model should generate code for the backend and a web form.
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.
(Optional) Follow naming for generated files. In this tutorial, names should be
main.go
andindex.html
. So, we need to renameToDoListApp.html
toindex.html
.To rename the file, press ShiftF6 or right-click a file and select Refactor | Rename.
We have now generated the initial application structure. Let us ensure it works by running it.
Click
in the gutter near the
main
function and select Run.
After you ensured that everything is working, you can stop it by pressing CtrlF2 or the Stop button on the toolbar.
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.
Find the
ToDoItem
struct inmain.go
.Select the whole struct and press Ctrl0\.
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 using the ai assistant popup](https://resources.jetbrains.com/help/img/idea/2024.3/go_using_the_ai_assistant_popup.png)
Second, to reflect this change in the web form, let us proceed with updating JavaScript in the HTML form.
Double-click
index.html
to open it in the editor.From the main menu, select View | Tool Windows | AI Assistant.
In the input field, select a model that you want to use. For example,
anthropic-claude-3.5-sonnet
.To add the context to the prompt form, click the plus icon and select two files:
main.go
andindex.html
.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.
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.
Click
in the gutter near the
main
function and select Run.
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 CtrlF2 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.
The AI Assistant can help you create tests for your code.
Open
main.go
in the editor, right-click anywhere in the code, and navigate to AI Actions | Generate Unit Tests.Wait for the generation to complete.
GoLand will open a window with generated test code.
If the generated code is satisfactory, click Accept all.
Click
in the gutter near the
main
function and select Run.
If you do not understand a code section, use the Explain code action for detailed explanations.
Select code that you want to explain.
Right-click the selection and navigate to AI Actions | Explain code.
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.
Select View | Tool Windows | Terminal from the main menu.
In the Terminal tool window, click the Options icon (
) and select Enable New Terminal.
Select View | Tool Windows | Terminal from the main menu.
In the Terminal tool window, click the Ask AI Assistant icon (
).
In the input field, write the following prompt and press Enter:
Run this Go application on port 8080 with race condition detection enabled.
Wait for the generation to complete.
GoLand will generate a command like
go run -race main.go -port 8080
.Press Enter to run the command.
After you ensured that everything is working, you can stop it by pressing CtrlF2 or the Stop button on the toolbar.
Finally, with our application ready, we can generate commit messages and push the changes to our version control system.
Select View | Tool Windows | Commit from the main menu.
Click the Generate Commit Message with AI Assistant icon (
).
Wait for the generation to complete.
In the Commit tool window, click Commit and Push.
Thanks for your feedback!