Build a simple API with Golang echo framework

Hi everyone, in this article i’m going to show tutorial on how to create simple API with Echo Golang framework

First thing that we need to do is to create project in golang by running this command

go mod init {your-package name}

example

go…


This content originally appeared on DEV Community and was authored by yanoandri

Hi everyone, in this article i'm going to show tutorial on how to create simple API with Echo Golang framework

First thing that we need to do is to create project in golang by running this command

go mod init {your-package name}

example

go mod init github.com/yanoandri/simple-golang-echo

your package name could be anything, but for this tutorial i'm using the url of my github repos for later

after running the command, there will be a file call go.mod and this time we will run this command to get echo dependencies

go get github.com/labstack/echo/v4

to make this dependencies recognized throughout the project run

go mod vendor

after the dependency is downloaded, let's start to create a file call server.go

package main

import (
    "net/http"

    "github.com/labstack/echo"
)

type HelloWorld struct {
    Message string `json:"message"`
}

func main() {
    e := echo.New()
    e.GET("/hello", Greetings)
    e.Logger.Fatal(e.Start(":3000"))
}

func Greetings(c echo.Context) error {
    return c.JSON(http.StatusOK, HelloWorld{
        Message: "Hello World",
    })
}

let's run the API that we just created, by command

go run server.go

Successfully run echo

Then We will test the API by request to http://localhost:3000/hello the response will be

{"message":"Hello World"}

Now, let's head back and handle any paramters or query inside the url, by modifying some of the line in the main function. let's add the function to handle query and parameters

func GreetingsWithParams(c echo.Context) error {
    params := c.Param("name")
    return c.JSON(http.StatusOK, HelloWorld{
        Message: "Hello World, my name is " + params,
    })
}

func GreetingsWithQuery(c echo.Context) error {
    query := c.QueryParam("name")
    return c.JSON(http.StatusOK, HelloWorld{
        Message: "Hello World i'm using queries and my name is " + query,
    })
}

Then in the main function, add this two line

e.GET("/hello/:name", GreetingsWithParams)
e.GET("/hello-queries", GreetingsWithQuery)

let's test it again by requesting the url with parameters
localhost:3000/hello/yano

{"message":"Hello World, my name is yano"}

and the second request using query with http://localhost:3000/hello-queries?name=yano

{"message":"Hello World i'm using queries and my name is yano"}

That's it for this tutorial, thank you for reading and happy coding :)

Source:


This content originally appeared on DEV Community and was authored by yanoandri


Print Share Comment Cite Upload Translate Updates
APA

yanoandri | Sciencx (2021-12-19T15:06:59+00:00) Build a simple API with Golang echo framework. Retrieved from https://www.scien.cx/2021/12/19/build-a-simple-api-with-golang-echo-framework/

MLA
" » Build a simple API with Golang echo framework." yanoandri | Sciencx - Sunday December 19, 2021, https://www.scien.cx/2021/12/19/build-a-simple-api-with-golang-echo-framework/
HARVARD
yanoandri | Sciencx Sunday December 19, 2021 » Build a simple API with Golang echo framework., viewed ,<https://www.scien.cx/2021/12/19/build-a-simple-api-with-golang-echo-framework/>
VANCOUVER
yanoandri | Sciencx - » Build a simple API with Golang echo framework. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/19/build-a-simple-api-with-golang-echo-framework/
CHICAGO
" » Build a simple API with Golang echo framework." yanoandri | Sciencx - Accessed . https://www.scien.cx/2021/12/19/build-a-simple-api-with-golang-echo-framework/
IEEE
" » Build a simple API with Golang echo framework." yanoandri | Sciencx [Online]. Available: https://www.scien.cx/2021/12/19/build-a-simple-api-with-golang-echo-framework/. [Accessed: ]
rf:citation
» Build a simple API with Golang echo framework | yanoandri | Sciencx | https://www.scien.cx/2021/12/19/build-a-simple-api-with-golang-echo-framework/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.