douza1373
2017-12-25 00:11
浏览 110
已采纳

在Golang中转换组合类型

I've been reading about type alias and composed structs in Golang. I want to be able to have two structs which are structurally identical but can be easily converted between each other.

I have a parent structure defined as:

type User struct {
    Email    string `json:"email"`
    Password string `json:"password"`
}

And a composed struct defined as:

type PublicUser struct {
    *User
}

I would expect that if I define a User:

a := User{
        Email:    "admin@example.net",
        Password: "1234",
    }

I could then perform the following type conversion:

b := (a).(PublicUser)

But it fails with an invalid type assertion:

invalid type assertion: a.(PublicUser) (non-interface type User on left)

How can I convert between structurally similar types in Go?

https://play.golang.org/p/I0VqrflOfXU

图片转代码服务由CSDN问答提供 功能建议

我一直阅读有关类型别名和Golang中的组合结构的信息。 我希望能够拥有两个结构相同但可以轻松在彼此之间转换的结构。

我的父结构定义为: < pre> type用户结构{ 电子邮件字符串`json:“ email”` 密码字符串`json:“ password”` }

以及定义为以下结构的组合结构:

  type PublicUser结构{
 * User 
} 
   
 
 

我希望如果我定义 User

  a:=用户{
电子邮件:“ admin@example.net”,
 密码:“ 1234”,
} 
   
 
 

然后我可以执行以下类型转换:

  b  :=(a)。(PublicUser)
   
 
 

但是失败,并带有无效的类型断言:

  invalid 类型断言:a。(PublicUser)(非接口类型用户在左侧)
   
 
 

如何在Go中的结构相似类型之间进行转换? \ n

https://play.golang.org/p/I0VqrflOfXU

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtu0363 2017-12-25 00:47
    已采纳

    Type assertions in Go let you tap into an interface's concrete type, not into structs:

    A type assertion provides access to an interface value's underlying concrete value.
    https://tour.golang.org/methods/15

    However, with slight modifications, this code works and probably behaves as you would expect:

    package main
    
    import (
        "fmt"
    )
    
    type User struct {
        Email    string `json:"email"`
        Password string `json:"password"`
    }
    
    type PublicUser User
    
    func main() {
        a := User{
            Email:    "admin@example.net",
            Password: "1234",
        }
        fmt.Printf("%#v
    ", a)
        // out: User{Email:"admin@example.net", Password:"1234"}
    
        b := PublicUser(a)
        fmt.Printf("%#v", b)
        // out PublicUser{Email:"admin@example.net", Password:"1234"}
    }
    

    Here, PublicUser is a redefinition of the User type; most importantly, it is a standalone type, shares the fields, but not the method set of User (https://golang.org/ref/spec#Type_definitions).

    Then, you can simply use the PublicUser type constructor, as you might have been doing similarly with string/[]byte conversions: foo := []byte("foobar").

    If, on the other hand, you were to use an actual type alias (type PublicUser = User) your output will list User as type for both instances: PublicUser is only a new name for the old thing, not a new type.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题