Programming

【Go Language】Setting Up an Echo Development Environment

Overview

In this article, we will set up a development environment for using the Echo backend framework with the Go programming language.
The main goals are as follows:

  • Running the application with hot reload, so the app updates automatically whenever the source code is modified.
  • Setting up the environment using Docker.

Before proceeding, it is recommended to refer to the following article to set up your environment using goenv. (At a minimum, it is assumed that Go is already installed on your PC.)

Please refer to the article below to download Docker Desktop in advance!

I hope this will be helpful for those who are looking to start using the Echo framework.

Creating a Go Project

Let's create a Go project by following the Echo documentation!

First, run the following commands.
In this example, we are creating a project named myapp, but feel free to use any name for your project.

mkdir myapp && cd myapp
go mod init myapp
go get github.com/labstack/echo/v4

What These Commands Do:

  • Create a directory for the Go project.
  • Initialize the Go project.
  • Download the Echo package.

Now, let's open the created project in VSCode!

Verify that go.mod and go.sum have been created, and then proceed to the next step!

ポイント

By the way, you can open the terminal in VSCode using Shift + Control + @! It's surprisingly handy, so be sure to remember it!

プロジェクトソースを実装

Following the Echo documentation, create the server.go file below to return "Hello, World!".

package main

import (
	"net/http"
	
	"github.com/labstack/echo/v4"
)

func main() {
	e := echo.New()
	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "Hello, World!")
	})
	e.Logger.Fatal(e.Start(":1323"))
}

Implementing the Dockerfile

Create a Dockerfile for building a container image of the Echo server for development purposes.
Name the file api.dockerfile.

The air package will be used for hot-reloading Go code.

FROM golang:1.23.2
WORKDIR /app

# download air package
RUN go install github.com/air-verse/air@latest

CMD ["air"]

This time, we’ve set the Go version to 1.23.2. Please check the version you're using with the go version command and adjust it accordingly.

Create the Air Configuration File

Create the air configuration file .air.toml to run the air package.

root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
  args_bin = []
  bin = "main"
  cmd = "go build -o ./main ."
  delay = 1000
  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "html"]
  include_file = []
  kill_delay = "0s"
  log = "build-errors.log"
  poll = false
  poll_interval = 0
  post_cmd = []
  pre_cmd = []
  rerun = false
  rerun_delay = 500
  send_interrupt = false
  stop_on_error = false

[color]
  app = ""
  build = "yellow"
  main = "magenta"
  runner = "green"
  watcher = "cyan"

[log]
  main_only = false
  time = false

[misc]
  clean_on_exit = false

[screen]
  clear_on_rebuild = false
  keep_scroll = true

Implementing the compose.yaml File

Create the compose.yaml file to run the image created in step 2.

services:
  api:
    container_name: myapp
    build:
      context: ./
      dockerfile: api.dockerfile
    volumes:
      - type: bind
        source: './'
        target: '/app'
    ports:
      - 1323:1323
    restart: always

The directory structure should look as follows:

Let’s give it a try!

Run the following commands to start the application.

docker compose up -d

Make sure Docker Desktop is running before executing the Docker commands!

Once the process is complete, open the following URL in your browser:

http://localhost:1323/

Next, let’s check if hot reloading is working!
Change the string "Hello, World!" in server.go to any string of your choice, save the file, and access the URL again.

If you see the updated output, the Echo development environment with hot reloading is successfully set up!
Now you’re ready to start developing your backend application!

Sponsored Link

  • Author

kaz

Full-stack Engineer specializing in Backend/Frontend/Cloud Infrastructure | Digital Nomad since June 2023, traveling the world | Sharing programming tips and insights | Posting travel updates on X

-Programming
-, ,