Get API not returning customerId

Hi All,

Added customerId field in the data model. Still the same 404 error. Not returning the customer ID.
Can anyone please help? Thank you so much in advance.

Code:

package main

import (

“encoding/json”

“fmt”

“log”

“net/http”

github.com/gorilla/mux

)

type Customer struct {

FullName string

City string

ZipCode string

CustomerId int

}

func main() {

router := http.NewServeMux()

router.HandleFunc(“/greet”, myHandler)

router.HandleFunc(“/customers”, customers)

router.HandleFunc(“/customers/{CustomerId}”, getCustomer)

log.Fatal(http.ListenAndServe(“localhost:8080”, router))

}

func myHandler(w http.ResponseWriter, c *http.Request) {

fmt.Fprint(w, “Hello World, Welcome to the world of GOlang REST API”)

}

func getCustomer(w http.ResponseWriter, c *http.Request) {

vars := mux.Vars(c)

fmt.Fprint(w, vars[“CustomerId”])

}

func customers(w http.ResponseWriter, c *http.Request) {

curt := []Customer{

{FullName: “Alice”, City:“Canada”, ZipCode: “100090”, CustomerId: 12345},

{FullName: “Bob”, City: “Japan”, ZipCode: “100095”, CustomerId:90000},

{FullName: “Marshall”, City:“Australia”, ZipCode: “100096”, CustomerId: 12345},

}

w.Header().Add(“Content-Type”,“application/json”)

json.NewEncoder(w).Encode(curt)

}

Output:

Ohh!
Hey Go Dev mate,
I am also facing the same problem. Have you been able to resolve it?

prog.go:5:1: illegal character U+201C ‘“’ (and 43 more errors)

@StephenCherry Yes able to resolve this one.
That is the right solution. I could figure it out seeing other examples on internet.
router := http.NewServeMux() – this is wrong
router := mux.NewRouter() – this is right.

2 Likes

@Sibert There is no problem with the quotes. The problem is the router line.

That is the right solution. I could figure it out seeing other examples on internet.
router := http.NewServeMux() – this is wrong
router := mux.NewRouter() – this is right.

1 Like

Yes and No. If you paste code as text into your question (compare text “fmt” with code "fmt") the double quotes will appear as wrong characters. Cut and paste code into playground they appear as illegal character U+201C ‘“’.

Please use the “code” icon or ``` (three backticks) to surround your code to lower the threshold for others to look at your code.

@Sibert I am new here. I agree.

1 Like

@shubhragrag Yes, It worked. In my code, I have already initialized it. So reassigning it throws an error. Thank you for the feedback.

1 Like