英文:
Go Language Program error
问题
我是一个新的Go语言程序员。以下是我的程序,但是我遇到了这个错误:
#command-line-arguments
.\helloworld.go:20: undefined: json.Marshall
有人可以告诉我为什么会出现这个错误吗?
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)
}
英文:
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)
}
答案1
得分: 5
输出清楚地告诉你问题出在哪里。
> undefined: json.Marshall
意味着没有这个方法的名称。另一方面,查看文档,该方法被称为
> func Marshal(v interface{}) ([]byte, error)
所以只需使用正确的名称,并学会如何调试,因为调试在软件工程中非常重要。
英文:
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.
答案2
得分: 2
更新你的程序
output, err := json.Marshal(message)
(Marshal
只有一个l
,参考链接:http://golang.org/pkg/encoding/json/#Marshal)。
英文:
Update your program
output, err := json.Marshal(message)
(Marshal
with one l
, http://golang.org/pkg/encoding/json/#Marshal).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论