英文:
Unmarshaling JSON into a UUID type
问题
我正在尝试使用json.Unmarshaler
接口将UUID解组到结构体上的uuid.UUID
字段。我创建了一个名为myUUID
的自定义类型,一切都正常,直到我尝试访问通常在uuid.UUID
上的方法时出现问题。我该如何处理这个问题?我对Go相对较新,所以也许我还不完全理解自定义类型。
package main
import (
"encoding/json"
"errors"
"fmt"
"code.google.com/p/go-uuid/uuid"
)
var jsonstring = `
{
"uuid": "273b62ad-a99d-48be-8d80-ccc55ef688b4"
}
type myUUID uuid.UUID
type Data struct {
uuid myUUID
}
func (u *myUUID) UnmarshalJson(b []byte) error {
id := uuid.Parse(string(b[:]))
if id == nil {
return errors.New("Could not parse UUID")
}
*u = myUUID(id)
return nil
}
func main() {
d := new(Data)
err := json.Unmarshal([]byte(jsonstring), &d)
if err != nil {
fmt.Printf("%s", err)
}
fmt.Println(d.uuid.String())
}
以下是翻译好的代码部分:
package main
import (
"encoding/json"
"errors"
"fmt"
"code.google.com/p/go-uuid/uuid"
)
var jsonstring = `
{
"uuid": "273b62ad-a99d-48be-8d80-ccc55ef688b4"
}
type myUUID uuid.UUID
type Data struct {
uuid myUUID
}
func (u *myUUID) UnmarshalJson(b []byte) error {
id := uuid.Parse(string(b[:]))
if id == nil {
return errors.New("无法解析UUID")
}
*u = myUUID(id)
return nil
}
func main() {
d := new(Data)
err := json.Unmarshal([]byte(jsonstring), &d)
if err != nil {
fmt.Printf("%s", err)
}
fmt.Println(d.uuid.String())
}
希望对你有帮助!如果还有其他问题,请随时提问。
英文:
I'm trying to use the json.Unmarshaler interface to unmarshal a UUID into a uuid.UUID field on a struct. I created a custom type called myUUID and everything works until I try to access methods that are normally on uuid.UUID. How can I handle this? I'm relatively new to Go, so maybe I just don't fully understand custom types just yet.
package main
import (
"encoding/json"
"errors"
"fmt"
"code.google.com/p/go-uuid/uuid"
)
var jsonstring = `
{
"uuid": "273b62ad-a99d-48be-8d80-ccc55ef688b4"
}
`
type myUUID uuid.UUID
type Data struct {
uuid myUUID
}
func (u *myUUID) UnmarshalJson(b []byte) error {
id := uuid.Parse(string(b[:]))
if id == nil {
return errors.New("Could not parse UUID")
}
*u = myUUID(id)
return nil
}
func main() {
d := new(Data)
err := json.Unmarshal([]byte(jsonstring), &d)
if err != nil {
fmt.Printf("%s", err)
}
fmt.Println(d.uuid.String())
}
答案1
得分: 8
你可能需要确保Data
结构体中的myuuid
变量是可见/导出的,即为"public"。对于类型别名MyUUID
,也是同样的情况(而不是myUUID
)。
type MyUUID uuid.UUID
type Data struct {
Uuid MyUUID
}
根据JSON和Go:
json包只访问结构体类型的导出字段(以大写字母开头的字段)。
名称中的首字母缩略词或首字母缩写词(例如"URL"或"NATO")应保持一致的大小写。例如,"URL"应该出现为"URL"或"url"(如"urlPony"或"URLPony"),而不是"Url"。这是一个例子:
ServeHTTP
而不是ServeHttp
。
当"ID"缩写为"identifier"时,也适用这个规则,所以应该写成"appID"而不是"appId"。
在你的情况下,这意味着:
type Data struct {
UUID MyUUID
}
英文:
You might want to make sure your myuuid
variable is visible/exported in the Data struct
: as in "public".
Same for the type alias MyUUID
(instead of myUUID
)
type MyUUID uuid.UUID
type Data struct {
Uuid MyUUID
}
From JSON and Go:
> The json package only accesses the exported fields of struct types (those that begin with an uppercase letter).
As commented by Ainar G, the style guide also recommends:
> Words in names that are initialisms or acronyms (e.g. "URL
" or "NATO
") have a consistent case.
For example, "URL
" should appear as "URL
" or "url
" (as in "urlPony
", or "URLPony
"), never as "Url
". Here's an example: ServeHTTP
not ServeHttp
.
> This rule also applies to "ID
" when it is short for "identifier," so write "appID
" instead of "appId
".
In your case, that would mean:
type Data struct {
UUID MyUUID
}
答案2
得分: 6
Go的自定义类型不继承方法。我使用自定义结构并附加了一个UnmarshalJSON来重构代码。
import (
"errors"
"strings"
"github.com/pborman/uuid"
)
type ServiceID struct {
UUID uuid.UUID
}
type Meta struct {
Name string `json:"name"`
Version string `json:"version"`
SID *ServiceID `json:"UUID"`
}
func (self *ServiceID) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
self.UUID = uuid.Parse(s)
if self.UUID == nil {
return errors.New("Could not parse UUID")
}
return nil
}
英文:
Go's custom types don't inherit methods. I refactored the code using a custom struct and attaching an UnmarshalJSON.
import (
"errors"
"strings"
"github.com/pborman/uuid"
)
type ServiceID struct {
UUID uuid.UUID
}
type Meta struct {
Name string `json:"name"`
Version string `json:"version"`
SID *ServiceID `json:"UUID"`
}
func (self *ServiceID) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
self.UUID = uuid.Parse(s)
if self.UUID == nil {
return errors.New("Could not parse UUID")
}
return nil
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论