Go语言程序错误

huangapple go评论92阅读模式
英文:

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).

huangapple
  • 本文由 发表于 2015年5月18日 10:01:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/30294089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定