I am a new Go Language programmer. Below is my program but I am getting this error:
#command-line-arguments
.\helloworld.go:20: undefined: json.Marshall
Can anyone tell why I get the error?
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type API struct {
Message string "json:message"
}
func main() {
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
message := API{"Hello, World!!!"}
output, err := json.Marshall(message)
if err != nil {
fmt.Println("Something went wrong")
}
fmt.Fprintf(w, string(output))
})
http.ListenAndServe(":8080", nil)
}
The output clearly tells you what is your problem.
undefined: json.Marshall
means that there is no method with this name. On the other side looking at the documentation the method is called
func Marshal(v interface{}) ([]byte, error)
So just use a correct name and learn how to debug because debugging is really important in software engineering.
Update your program
output, err := json.Marshal(message)
(Marshal
with one l
, http://golang.org/pkg/encoding/json/#Marshal).