Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 20, 2022 01:12 pm GMT

Initial Setup

We will need Go 1.14+ installed in our system. You can check the Go version in your terminal by writing go version. If you still don't have Go on your system, you can easily download one with brew install go using Homebrew. Or follow the official documentation on how to download and install Go.

Lets initialize the project.

go mod init go-redis-url-shortener

This will create go.mod file in the project folder.

After successfully initializing our project, the next step is creating the main.go file and add the following code.

package mainimport "fmt"func main() {    fmt.Printf("Welcome to Go URL Shortener with Redis !")}  

If we run go run main.go in the terminal inside the project directory (where our main.go file is located) we should see this output:

Welcome to Go URL Shortener with Redis !

Amazing! The project setup was successful.

Now lets add Echo, an open-source Go web application framework, so we can easily build our web application. For more details visit their website.

go get github.com/labstack/echo/v4

This will create go.sum file in the project folder.

All right, all right. Now we are ready to start the web server and return some data in JSON format. Update the main.go file to reflect these changes.

package mainimport (    "github.com/labstack/echo/v4"    "net/http")func main() {    e := echo.New()    e.GET("/", func(c echo.Context) error {        return c.JSON(http.StatusOK, map[string]interface{}{            "message": "Welcome to Go URL Shortener with Redis !",        })    })    e.Logger.Fatal(e.Start(":1323"))}

Run again the main.go file (command: go run main.go ) and open http://localhost:1323/ in your browser or other rest client tool. The output should look like this.

{  "message": "Welcome to Go URL Shortener with Redis !"}

If you got this JSON response, its time to celebrate. This means that our setup was done successfully.

Originally published at projectex.dev


Original Link: https://dev.to/janedzumerko/how-to-build-a-url-shortener-in-go-with-redis-initial-setup-3kf2

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To