DEV Community

Madhav Jha
Madhav Jha

Posted on

3 1

A simple golang web server using awesome echo

#go

If you are like me and used golang at some point in the past and haven't kept up with the language changes, the most significant change has been the introduction of go modules.

A very good introduction is available here. But the TLDR version is as follows.

In a new directory, create a file main.go.

package main

import (
    "net/http"

    "github.com/labstack/echo/v4"
    "github.com/labstack/echo/v4/middleware"
)

func main() {
    // Echo instance
    e := echo.New()

    // Middleware
    e.Use(middleware.Logger())
    e.Use(middleware.Recover())

    // Routes
    e.GET("/", hello)

    // Start server
    e.Logger.Fatal(e.Start(":1323"))

}

func hello(c echo.Context) error {
    return c.String(http.StatusOK, "hello world")
}

Run the following commands inside the directory.

go mod init example.com/hello
go get github.com/labstack/echo/v4

This will create files go.mod and go.sum which are similar to package.json and package-lock.json in node/npm world.

At this point you are ready to build. Note the binary built is called hello which is what you called the package.

go build .
./hello

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay