• Download
  • Contact
  • Terms of Service
  • Privacy Policy
  • About US
Codershood
  • Demos
  • Plugins
  • Angular
  • NodeJs
  • GO lang
  • Others
No Result
View All Result
Codershood
  • Demos
  • Plugins
  • Angular
  • NodeJs
  • GO lang
  • Others
No Result
View All Result
Codershood
No Result
View All Result

Send a JSON response in golang using Gorilla mux

by Shashank Tiwari
February 29, 2020
in GO lang
0
< 1 minute
Send a JSON response in golang using Gorilla mux

Generally, we use the response.Write() method to send the response to the API call. In the below code, we are sending a response by converting a string to byte.

Here is the sample code.

package main

import (
"net/http"

"github.com/gorilla/mux"
)

func main() {
route := mux.NewRouter()

route.HandleFunc("/", func(response http.ResponseWriter, request *http.Request) {
response.WriteHeader(http.StatusOK)
response.Write([]byte("Hello World"))
})

http.ListenAndServe(":8000", route)
}

 




 

But there is a better way to send a response from the API. You can send Json response using json.Marshal, This method will give the Json string that can be sent to the Client.

Here is the sample code.

package main

import (
"encoding/json"
"fmt"
"net/http"

"github.com/gorilla/mux"
)

type person struct {
Name     string
LastName string
Age      uint8
}

func sendResponse(response http.ResponseWriter, request *http.Request) {
person := person{Name: "Shashank", LastName: "Tiwari", Age: 30}

jsonResponse, jsonError := json.Marshal(person)

if jsonError != nil {
fmt.Println("Unable to encode JSON")
}

fmt.Println(string(jsonResponse))

response.Header().Set("Content-Type", "application/json")
response.WriteHeader(http.StatusOK)
response.Write(jsonResponse)
}

func main() {
route := mux.NewRouter()

route.HandleFunc("/", sendResponse)

http.ListenAndServe(":8000", route)
}

In the above code, we converted the person structs to JSON string using json.Marshal.

Checkout more GoLang tutorials.

Tags: GoGo serverGoLang apiGolang server
Previous Post

Serving Static Files in GoLang using Gorilla Mux

Next Post

Building a URL Shortener with GoLang and Redis

Related Posts

Real time private chatting app using React, Golang and mongodb banner-part 2
GO lang

Real time private chatting app using GoLang, React and mongodb – Part 2

July 4, 2020
Real time private chatting app using React, Golang and mongodb banner
GO lang

Real time private chatting app using GoLang, React and Mongodb – Part 1

July 4, 2020
Sending message to specific user with GoLang WebSocket
GO lang

Sending message to specific user with GoLang WebSocket

August 6, 2023
Next Post
Building a URL Shortener with GoLang and Redis

Building a URL Shortener with GoLang and Redis

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *




https://codershood.info

www.codershood.info programming blog dedicated to providing high-quality coding tutorial and articles on web development, Angular, React, Laravel, AngularJs, CSS, Node.js, ExpressJs and many more. We also provide ebook based on complicated web application along with the source code.

  • Download
  • Contact
  • Terms of Service
  • Privacy Policy
  • About US

www.codershood.info is licensed under a Creative Commons Attribution 4.0 International License.

No Result
View All Result
  • Demos
  • Plugins
  • Angular
  • NodeJs
  • GO lang
  • Others

www.codershood.info is licensed under a Creative Commons Attribution 4.0 International License.